Home > Software engineering >  ASP.Net Getting null value on submit in ActionResult
ASP.Net Getting null value on submit in ActionResult

Time:05-10

I have created a controller and an edit page. The page should allow the user to update a subscription and save. Everything loads fine but when I submit the page the value is always null.

  [HttpPost]
        public ActionResult Edit(ReportSubscription reportSubscription)
        {
            var rs = reportSubscription;
       
        }

That and have a view that submits to it.

@model api.ReportSubscription

@{
    ViewBag.Title = "Edit Subscription";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


@using (Html.BeginForm("Edit", "Subscription" ))
{

 <div >
        @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-3" })
        <div >

            <div  style="background-color:lightgray; width:500px">
                @Html.DisplayTextFor(model => model.Description)

            </div>
        </div>
    </div>

<div >
        <div >
            <input type="submit" name="saveButton" value="Save"  />
        
   
        </div>
    </div>
}

Loads fine but when I submit the value in the controller is always null.

CodePudding user response:

Try to use @Html.HiddenFor() to create a hidden field which will help the default model binder to working properly:

@using (Html.BeginForm("Edit", "Subscription"))
{
    @Html.HiddenFor(m => m.Description)
    ...
}
  • Related