繁体   English   中英

c# MVC ViewModel 没有传递给控制器

[英]c# MVC ViewModel is not passing to controller

我有一个模型:

public class PersonViewModel
{
    public int Id { get; set; }
    [MaxLength(25)]
    public string Firstname { get; set; }
    [MaxLength(50)]
    public string Surname { get; set; }
}

以及一个包含一个实例和上述列表的 ViewModel:

public class PeopleSearchViewModel
{
    public PersonViewModel Person { get; set; }
    public List<PersonViewModel> People { get; set; }
}

然后我的观点:

@model PeopleSearchViewModel

@using (Html.BeginForm("Search", "Home", FormMethod.Post))
{
    @Html.LabelFor(m => m.Person.Firstname)
    @Html.HiddenFor(m => m.Person.Firstname)
    @Html.TextBoxFor(m => m.Person.Firstname)
    @Html.ValidationMessageFor(m => m.Person.Firstname)

    <input type="submit" value="Search" id="Whatever"/>
}

最后是控制器:

[HttpPost]
public ActionResult Search(PeopleSearchViewModel theModelIsntPassing)
{
}

模型没有在表单提交时传递给控制器​​? 或者可能是这样,但是没有填充各个属性。

肯定会调用 ActionResult Search方法,只是theModelIsntPassing的嵌套属性中没有值

不知道为什么你有 HiddenFor 和 TextBoxFor 的名字。 请在您的视图中尝试评论/删除@Html.HiddenFor(m => m.Person.Firstname)语句

@model PeopleSearchViewModel

@using (Html.BeginForm("Search", "Home", FormMethod.Post))
{
    @Html.LabelFor(m => m.Person.Firstname)
    @*@Html.HiddenFor(m => m.Person.Firstname)*@
    @Html.TextBoxFor(m => m.Person.Firstname)
    @Html.ValidationMessageFor(m => m.Person.Firstname)

    <input type="submit" value="Search" id="Whatever"/>
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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