c#MVC视图模型未传递给控制器
我有一个模型:c#MVC视图模型未传递给控制器,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个模型: 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 PeopleSea
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; }
}
表单提交时模型未传递给控制器?
或者可能是这样,但是没有填充单个属性
ActionResult
Search
方法肯定正在被调用,只是ModelsNTPassing
在其嵌套属性中没有值不确定为什么要同时使用HiddenFor和TextBoxFor作为FirstName。
请尝试在视图中注释/删除@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"/>
}
@model PeopleSearchViewModel
@使用(Html.BeginForm(“搜索”、“主页”、FormMethod.Post))
{
@LabelFor(m=>m.Person.Firstname)
@*@Html.HiddenFor(m=>m.Person.Firstname)*@
@Html.TextBoxFor(m=>m.Person.Firstname)
@Html.ValidationMessageFor(m=>m.Person.Firstname)
}
不确定为什么您的FirstName同时具有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"/>
}
@model PeopleSearchViewModel
@使用(Html.BeginForm(“搜索”、“主页”、FormMethod.Post))
{
@LabelFor(m=>m.Person.Firstname)
@*@Html.HiddenFor(m=>m.Person.Firstname)*@
@Html.TextBoxFor(m=>m.Person.Firstname)
@Html.ValidationMessageFor(m=>m.Person.Firstname)
}
PatientSearchPageViewModel是要接收的正确型号吗?我们能看看它的定义吗?对不起,那是个打字错误。它应该与类和视图中定义的PeopleSearchViewModel
相同。ActionResult函数必须与Html.BeginForm
中的第一个参数匹配。请看这里的一个例子:不,对不起,这也是以前版本的一个拼写错误,现在更正了。在我的控制器上调用了正确的操作,但是模型没有通过。在Request.Forms对象中,参数作为key/val对存在Person.FirstName
要接收的模型是否正确?我们能看看它的定义吗?对不起,那是个打字错误。它应该与类和视图中定义的PeopleSearchViewModel
相同。ActionResult函数必须与Html.BeginForm
中的第一个参数匹配。请看这里的一个例子:不,对不起,这也是以前版本的一个拼写错误,现在更正了。在我的控制器上调用了正确的操作,但是模型没有通过。在Request.Forms对象中,参数作为key/val对存在<代码>人.名谢谢!这起作用了。在我看的教程中,它说这是它工作的必要条件。但也许里面没有一个文本框。。。我再看看。谢谢!这起作用了。在我看的教程中,它说这是它工作的必要条件。但也许里面没有一个文本框。。。我再看看。
@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"/>
}