user13520940
user13520940

Reputation: 143

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.

Upvotes: 2

Views: 370

Answers (2)

Grewal
Grewal

Reputation: 16

Use input type elements like (@html.editorfor) in cshtml fileπŸ‘πŸ»πŸ‘πŸ»

Upvotes: 0

Victor
Victor

Reputation: 8935

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)
    ...
}

Upvotes: 1

Related Questions