MVC部分视图不提交隐藏字段值

问题描述 投票:1回答:2

我正在开发一个MVC 5 Web应用程序。其中一个视图引用了部分视图,就像这样

<div class="row">
    <div class="panel-body" id="TimeEntryPartial">
       @Html.Partial("_TimeEntryPartial", Model.TimeEntry)
    </div>
</div>

然后,“部分视图”显示一个包含多个字段的表单,其中一个字段是隐藏字段,就像这样

@using (Html.BeginForm("AddTimeEntry", "TimeEntry", FormMethod.Post, new { name = "frm", id = "frm" }))
{

@Html.HiddenFor(m => m.SelectedUserID)

<div id="AddButtons">
    <div class="col-sm-12">
        <input type="submit" id="btnAdd" name="btnAdd" class="btn btn-warning btn-block" value="Add Entry">
    </div>
</div>

}

当用户提交表单时,在Controller中调用HttpPost Action,就像这样

[HttpPost]
public ActionResult AddTimeEntry(TimeEntryViewModel model)
{
    if(ModelState.IsValid)
    {
        //code here
        int userID = model.SelectedUserID;
    }
}

除了SelectedUserID之外,表单中的所有值都在ViewModel中成功传递。这始终为0。

我认为它可能与隐藏的字段有关,因此我将其更改为文本框,我可以看到其中的值。但是,再次调用HttpPost Action时,属性SelectedUserID的值未包含在ViewModel中。

有没有人对如何解决这个问题有任何建议?

谢谢你的帮助。

asp.net-mvc asp.net-mvc-5 hidden-field asp.net-mvc-partialview
2个回答
1
投票

检查你的poco。如果字段未正确设置为属性,则自动布线将忽略它。


0
投票

我有同样的问题。我通过摆脱局部视图解决了它。

一切正常。

© www.soinside.com 2019 - 2024. All rights reserved.