简体   繁体   中英

Model is null on post back in ASP.NET MVC

I am trying to generate a list of checkboxes and post the selected item to post method of my controller but my view model is null.

Please see below code and help.

The view get invoke with the following button on another page -

<button class="btn btn-primary" id="historicalrecords"  
        onclick="location.href='@Url.Action("HistoricalWorkSubmissions", "Main", new {id= @Model.MessageIdsCombined.FirstOrDefault()})'">View Historical Works</button>

Model WorkSubmissions.cs :

public class HistoricalWorkSubmission
{
    public string Society { get; set; }
    public string Rsa { get; set; }
    public DateTime ProcessingTime { get; set; }
    public bool isSelected { get; set; }

}

public class HistoricalWorkSubmisssionViewModel
{
    public List<HistoricalWorkSubmission> Submissions { get; set; }
}

Get method in MainController :

[HttpGet]
public async Task<ActionResult> HistoricalWorkSubmissions(string id)
{
    WorkSearchViewModel workSearchViewModel = new WorkSearchViewModel
            {
                MessageId = id
            };

    var workSubmissions = await _swvManager.SearchAllWorkSubmissionsAsync(workSearchViewModel).ConfigureAwait(true);

    return View("~/Views/Main/HistoricalWorkSubmissions.cshtml", workSubmissions);
}

HistoricalWorkSubmissions.cshtml :

 @model SWV.WorkPicture.UI.Models.HistoricalWorkSubmisssionViewModel

@{
    ViewBag.Title = "HistoricalSubmissions";
}

<h2>HistoricalSubmissions</h2>
@using (Html.BeginForm("HistoricalWorkSubmissions", "Main", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken();
    <fieldset>
        <div>
            <table class="table-bordered">
                @*@foreach (var submission in Model.Submissions)*@
                @for (int i=0; i < Model.Submissions.Count(); i++)
                {
                    var bmiWorks = Model.Submissions[i].Society + Model.Submissions[i].Rsa + "   " + Model.Submissions[i].ProcessingTime;
                    <tr>
                        <td>
                            @Html.CheckBoxFor(m => Model.Submissions[i].isSelected)
                            @Html.Label(bmiWorks)
                            @Html.HiddenFor(m => Model.Submissions[i])

                        </td>
                    </tr>
                }
            </table>
            <input class="button btn-primary" type="submit" value="Save"/>
        </div>
    </fieldset>
}

And finally post method:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> HistoricalWorkSubmissions(HistoricalWorkSubmisssionViewModel workSubmissions)
{
    WorkSearchViewModel workSearchViewModel = new WorkSearchViewModel();
    workSearchViewModel.SwvId = "5124cfb4-afe8-4783-ab97-b9fbaaf6737d";

    var workPicturesx = await _swvManager.SearchAllWorkSubmissionsAsync(workSearchViewModel).ConfigureAwait(true);
           
    return View("~/Views/Main/HistoricalWorks.cshtml");
}

POST- 邮政

The value of abc is null in debugger. Please help.

Make sure the field names in the post match the model. The Label is not an issue, but CheckBoxFor and HiddenFor would generate Html input tags in the form posted to server.

@Html.CheckBoxFor(m => Model.Submissions[i].isSelected)
@Html.Label(bmiWorks)
@Html.HiddenFor(m => Model.Submissions[i].Society)
@Html.HiddenFor(m => Model.Submissions[i].Rsa)
@Html.HiddenFor(m => Model.Submissions[i].ProcessingTime)   

Also I've removed the hidden field for submission because it's an object in your model, while we need to generate tags for each property instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM