简体   繁体   English

部分视图中的强类型视图不起作用

[英]Strongly typed view in partial view not working

I am having an issue with my form submission in strongly typed view. 我在强类型视图中提交表单时遇到问题。 My view is partial and embedded in another strongly typed view that has no form in it. 我的视图是部分视图,并嵌入到另一个没有格式的强类型视图中。 My parent view controller returns type JobsListViewModel. 我的父视图控制器返回类型JobsListViewModel。

public class JobsListViewModel
{
        public IEnumerable<JobPost> JobPosts { get; set; }
        public PagingInfo PagingInfo { get; set; }
        public SearchTerms searchTerms { get; set; }
 }

Note: JobPosts is a model class itself and i use it on my parent view. 注意:JobPosts本身就是一个模型类,我在父视图中使用它。 searchterms is a model class which i use in my partial view. searchterms是我在局部视图中使用的模型类。 The form in my partial view is like below. 我的局部视图中的表单如下所示。

 @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"})
   ....                               

}

My Post controller method is like below. 我的Post控制器方法如下所示。

[HttpPost]
public ViewResult List(SearchTerms search,int page = 1)
{
 ....
}

On my form submit , it calls my Post controller method. 在我的表单commit上,它调用我的Post控制器方法。 However, SearchTerms is always empty. 但是,SearchTerms始终为空。 It totally does not bind. 它完全不绑定。 Please is there any where i can acheive this ? 请问有什么我可以做到的吗? does it have to do with my view being a partial view ? 与我的观点是局部观点有关吗? any help would be appreciated. 任何帮助,将不胜感激。

You partial is generating controls based on typeof JobsListViewModel . 您部分是基于JobsListViewModel类型生成控件。 For example you textbox will look like 例如,您的文本框看起来像

<input type="text" name="searchTerms.searchText" ... />

but your posting back only to typeof SearchTerms . 但是您的发布仅返回typeof SearchTerms Typeof SearchTerms does not contain a property searchTerms so binding fails. TYPEOF SearchTerms不包含财产searchTerms所以绑定失败。

You can use the Prefix property of the [Bind] attribute to effectively ignore the prefix 您可以使用[Bind]属性的Prefix属性有效地忽略前缀

[HttpPost]
public ViewResult List([Bind(Prefix="searchTerm")]SearchTerms search, int page = 1)

Alternatively you can post back the model the view was based on 或者,您可以回发视图基于的模型

[HttpPost]
public ViewResult List(JobsListViewModel model, int page = 1)

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

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