skip to Main Content

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.

2

Answers


  1. 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)
        ...
    }
    
    Login or Signup to reply.
  2. Use input type elements like (@html.editorfor) in cshtml fileπŸ‘πŸ»πŸ‘πŸ»

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search