Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc Asp.net MVC2 ModelBindingContext.ModelName为空_Asp.net Mvc_Asp.net Mvc 2 - Fatal编程技术网

Asp.net mvc Asp.net MVC2 ModelBindingContext.ModelName为空

Asp.net mvc Asp.net MVC2 ModelBindingContext.ModelName为空,asp.net-mvc,asp.net-mvc-2,Asp.net Mvc,Asp.net Mvc 2,我甚至不太确定从哪里开始解释这个问题。在过去的10个小时里,我一直在研究这个问题,却不知道根本原因是什么。如果需要任何额外的细节,我很乐意提供。我只是在猜测在这一点上什么是相关的 我有一个MVC2站点,路由由Steve Hodgkiss的奇妙软件包设置,使用带有嵌套控制器的默认路由设置(例如/customer/{custid}/location/{locid}等等) 在这方面,我有一个特别的模型,它给了我一些问题。出于某种原因,当创建页面将数据发送回我的服务器时,ModelBindingCont

我甚至不太确定从哪里开始解释这个问题。在过去的10个小时里,我一直在研究这个问题,却不知道根本原因是什么。如果需要任何额外的细节,我很乐意提供。我只是在猜测在这一点上什么是相关的

我有一个MVC2站点,路由由Steve Hodgkiss的奇妙软件包设置,使用带有嵌套控制器的默认路由设置(例如/customer/{custid}/location/{locid}等等)

在这方面,我有一个特别的模型,它给了我一些问题。出于某种原因,当创建页面将数据发送回我的服务器时,ModelBindingContext对象中的ModelName属性传递给DefaultModelBinder(我的自定义类继承自DefaultModelBinder,用于处理从存储库抓取对象)。这种情况只发生在这一个模型上。我一点也看不出区别

破碎的模型

public class RemedialItem : Entity
{
   public virtual int Id { get; set; }
....
工作模式:

public class Customer : Entity
{
   public virtual int Id { get; set; }
....
实体只是一个空类,用作反射使用的标记

RemedialItemController.cs

[HttpGet]
public ActionResult New(int? locationId, int? applianceId)
{
   var model = ViewModelFactory.Create<CreateRemedialItemViewModel>();
   model.Categories = (from c in repository.Query<RemedialItemCategory>()
                       orderby c.Name
                       select c).ToList();
   model.RemedialItem = new RemedialItem();
   return View(model);
}
[HttpGet]
  public ActionResult New()
  {
     var viewModel = ViewModelFactory.Create<SingleCustomerViewModel>();
     viewModel.Customer = new Customer();

     return View(viewModel);
  }
ViewModelFactory是一个注入类,用于设置所有视图模型通用的一些基本属性(主要是当前登录的用户和用户详细信息)

损坏的viewmodel:

public class CreateRemedialItemViewModel : ViewModelBase
{
   public RemedialItem RemedialItem { get; set; }
   public IList<Location> Locations { get; set; }
   public IList<Appliance> Appliances { get; set; }
   public IList<RemedialItemCategory> Categories { get; set; }
}
ViewModelBase包含由ViewModelFactory填充的一些属性

RemedialItem的W新视图中的断开表单:

 <% using(Html.BeginForm("Create","RemedialItem",FormMethod.Post))
 {%>
 <%: Html.AntiForgeryToken() %>   
 <fieldset>      
   <legend>General</legend>      
   <div>
      <%: Html.LabelFor(m=>m.RemedialItem.Category) %>
      <%:Html.DropDownListFor(m=>m.RemedialItem.Category.Id, new  SelectList(Model.Categories,"Id","Name")) %>
   </div>
   <div>
      <%: Html.LabelFor(m=>m.RemedialItem.Item) %>
      <%: Html.TextAreaFor(m=>m.RemedialItem.Item) %>
   </div>
   <div>
      <%: Html.LabelFor(m=>m.RemedialItem.Note) %>
      <%: Html.TextAreaFor(m=>m.RemedialItem.Note) %>
   </div>
   <input type="submit" value="Create Item" />
 </fieldset>
 <%}%>  

一般的
m、 补救措施项目类别)%>
m、 RemedialItem.Category.Id,新选择列表(Model.Categories,“Id”,“Name”))%>
m、 补救措施项目)%>
m、 补救措施项目)%>
m、 补救措施(注)%>
m、 补救措施(注)%>
新的工作视图:

<% using (Html.BeginForm("Create","Customer",FormMethod.Post)) {%>
    <%: Html.ValidationSummary(true) %>
    <%:Html.AntiForgeryToken() %>
    <fieldset>
        <legend>Fields</legend>
        <p>
        <%: Html.LabelFor(m=>m.Customer.Name) %>
        <%: Html.TextBoxFor(m=>m.Customer.Name) %>
        </p>
        <p>
        <%: Html.LabelFor(m=>m.Customer.Street) %>
        <%: Html.TextBoxFor(m=>m.Customer.Street) %>
        </p>
        [...tl;dr...]
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

领域

m、 客户名称)%%>
m、 客户名称)%%>

m、 顾客(街道)%> m、 顾客(街道)%>

[…TL;博士]

两者都产生类似的字段名:

破碎的:

<label for="RemedialItem_Item">Item</label>
<textarea cols="20" id="RemedialItem_Item" name="RemedialItem.Item" rows="2">
</textarea>
项目
工作:

<label for="Customer_Name">Name</label>
<input id="Customer_Name" name="Customer.Name" type="text" value="" />
名称
简而言之,我为过长的代码转储道歉: 在创建表单上发回的工作组内容的ModelName设置为
Customer
。坏掉的东西是一根空绳子


有什么我遗漏的吗?以前有人遇到过类似的情况吗?

我发现了这个问题。在ViewModel中,保存要显示的
RemedialItem
实例的属性称为
RemedialItem
。在它发布到的操作中,获取
RemedialItem
实例的参数被称为
item
,这破坏了一切

简而言之,使用ViewModels时,请确保从ViewModel中获取对象的参数名称与ViewModel中的特性名称相同

我的一天过去了

<label for="Customer_Name">Name</label>
<input id="Customer_Name" name="Customer.Name" type="text" value="" />