部分视图中的强类型视图不起作用
Strongly typed view in partial view not working
我在强类型视图中提交表单时遇到问题。我的观点是局部的,嵌入到另一个没有形式的强类型视图中。我的父视图控制器 returns 输入 JobsListViewModel。
public class JobsListViewModel
{
public IEnumerable<JobPost> JobPosts { get; set; }
public PagingInfo PagingInfo { get; set; }
public SearchTerms searchTerms { get; set; }
}
注意:JobPosts 本身就是一个模型 class,我在父视图中使用它。 searchterms 是我在部分视图中使用的模型 class。
我的部分视图中的表单如下所示。
@using (Html.BeginForm("List", "Search",
FormMethod.Post, new { @class="form-group text-right" }))
{
@Html.TextBoxFor(x=>x.searchTerms.searchText,new {@class="form-control"})
@Html.DropDownListFor(func=>func.searchTerms.JobFunction,ViewBag.JobFunction as SelectList,"Job Function",new {@class="dropdown"})
....
}
我的 Post 控制器方法如下所示。
[HttpPost]
public ViewResult List(SearchTerms search,int page = 1)
{
....
}
在我的表单提交中,它调用了我的 Post 控制器方法。但是,SearchTerms 始终为空。它完全不绑定。请问有什么地方可以实现的吗?这与我的观点是部分观点有关吗?任何帮助将不胜感激。
您部分正在根据 typeof JobsListViewModel
生成控件。例如你的文本框看起来像
<input type="text" name="searchTerms.searchText" ... />
但您的 posting 仅返回到 typeof SearchTerms
。 Typeof SearchTerms
不包含 属性 searchTerms
因此绑定失败。
可以使用[Bind]
属性的Prefix
属性有效忽略前缀
[HttpPost]
public ViewResult List([Bind(Prefix="searchTerm")]SearchTerms search, int page = 1)
或者您可以post返回视图所基于的模型
[HttpPost]
public ViewResult List(JobsListViewModel model, int page = 1)
我在强类型视图中提交表单时遇到问题。我的观点是局部的,嵌入到另一个没有形式的强类型视图中。我的父视图控制器 returns 输入 JobsListViewModel。
public class JobsListViewModel
{
public IEnumerable<JobPost> JobPosts { get; set; }
public PagingInfo PagingInfo { get; set; }
public SearchTerms searchTerms { get; set; }
}
注意:JobPosts 本身就是一个模型 class,我在父视图中使用它。 searchterms 是我在部分视图中使用的模型 class。 我的部分视图中的表单如下所示。
@using (Html.BeginForm("List", "Search",
FormMethod.Post, new { @class="form-group text-right" }))
{
@Html.TextBoxFor(x=>x.searchTerms.searchText,new {@class="form-control"})
@Html.DropDownListFor(func=>func.searchTerms.JobFunction,ViewBag.JobFunction as SelectList,"Job Function",new {@class="dropdown"})
....
}
我的 Post 控制器方法如下所示。
[HttpPost]
public ViewResult List(SearchTerms search,int page = 1)
{
....
}
在我的表单提交中,它调用了我的 Post 控制器方法。但是,SearchTerms 始终为空。它完全不绑定。请问有什么地方可以实现的吗?这与我的观点是部分观点有关吗?任何帮助将不胜感激。
您部分正在根据 typeof JobsListViewModel
生成控件。例如你的文本框看起来像
<input type="text" name="searchTerms.searchText" ... />
但您的 posting 仅返回到 typeof SearchTerms
。 Typeof SearchTerms
不包含 属性 searchTerms
因此绑定失败。
可以使用[Bind]
属性的Prefix
属性有效忽略前缀
[HttpPost]
public ViewResult List([Bind(Prefix="searchTerm")]SearchTerms search, int page = 1)
或者您可以post返回视图所基于的模型
[HttpPost]
public ViewResult List(JobsListViewModel model, int page = 1)