Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ASP.Net Getting null value on submit in ActionResult

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 class="form-group">
        @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-3" })
        <div class="col-md-10">

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

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

<div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" name="saveButton" value="Save" class="btn btn-primary" />
        
   
        </div>
    </div>
}

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
    ...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading