2013-06-13 23 views
1

我有一个我已经列出的视图。从Razor视图调用参数构造函数

当我发帖的形式,组织的默认构造函数是越来越调用。 但是,我想要调用另一个构造函数,它需要一个Party对象。

如何做到这一点的剃刀或任何其他使用MVC,请告知。

我的代码:

public O(Pobj) 
     : this() 
    { 
     P= obj; 
    } 

查看:

@using P.M.O 


@model IEnumerable<O> 
@{ 
ViewBag.Title = "Details"; 
} 

<table> 
    <tr> 
    <th> 
     @Html.Raw("Caption") 
    </th> 
    <th></th> 
</tr> 
<tr> 
<td colspan="4"> 
    @using (Html.BeginForm("Edit", "O", FormMethod.Post)) 
    { 
     <table> 
      <tr> 
      @foreach (var item in Model) 
      { 

       <td class="txt"> 
        @Html.TextBox("C", item.GetValForProp<string>("C"), new { @class = "txt" }) 
       </td> 
       <td class="txt"> 
        @Html.TextBox("N", item.GetValForProp<string>("N"), new { @class = "txt" }) 
       </td> 
       <td class="txt"> 
        @Html.TextBox("D", item.GetValForProp<string>("D"), new { @class = "txt" }) 
       </td> 
       <td> 
        <button type="submit">Edit</button> 
       </td> 
      } 
      </tr> 
     </table> 
    } 
</td> 

加说还没有解决上面的问题,我有另外一个问题。

我的组织是另一个对象党的孩子。 所以它将有一个与派对表细节对应组织的属性派对(orgobj.Party有派对对象)。

当我点击编辑,在我的控制器的orgobj.Party是零和编辑不工作。 异常:发生参照完整性约束违规:定义参照约束的属性值在关系中的主体和从属对象之间不一致。

请告知,如果我做的东西或我怎么能模拟oganization的绑定方可以在编辑控制器提供???

+0

有趣的问题,但我敢肯定,你不能这样做。为什么不在施工后就设置Party属性? – Serge

+0

您需要为“组织”注册一个自定义'ModelBinder'才能这样做。但是,我仍然很好奇你会传递给期望'Party'的构造函数吗? – haim770

+0

我可以将Party对象传递给存在于Organization对象(orgObj.Party)中的构造函数。如何使用ModelBinder,可以建议吗? – mmssaann

回答

1

这有点棘手,但可以在某些情况下完成。根据您的代码,我假设您启动组织对象时将有一个Party对象可用。

我能想到的解决这个唯一的办法是通过自定义ModelBinder的。在我的例子中,我假设你有一个传入的PartyId参数,我们可以使用它来加载一个Party对象,然后启动组织对象。你可能会以不同的方式检索Party对象,但这并不重要。我只是用ModelBinder展示了一种方法。

public class OrganizationModelBinder : DefaultModelBinder 
{ 
    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) 
    { 
     // Try to get incoming PartyId 
     int partyId = 0; 
     ValueProviderResult value = bindingContext.ValueProvider.GetValue("PartyId"); 
     if (value == null) 
     { 
      throw new Exception("Missing party id"); 
     } 
     int.TryParse(value.AttemptedValue, out partyId); 

     // Load Party object from PartyId 
     DbContext db = new DbContext(); 
     Party party = db.Parties.FirstOrDefault(p => p.PartyId == partyId); 

     if (party == null) 
     { 
      throw new Exception("Invalid party"); 
     } 


     return new Organization(party); 

     // If you want to create an instance dynamically based upon the modelType 
     // passed in as an parameter then you'll have to use the following return 
     // line instead. 
     // return Activator.CreateInstance(modelType, party); 
     // 
     // If using this second return line you just have to add one 
     // ModelBinders.Binders.Add(...) line for each and every one of the models 
     // you want to use. 
    } 
} 

您还可以在Global.asax.cs

ModelBinders.Binders.Add(typeof(Organization), new OrganizationModelBinder()); 

这可以为你工作登记在Application_Start()模型粘结剂......但是这一切都取决于你想要做什么。

+0

Ohlin,谢谢你的回复。你可以建议把这个活页夹放在什么位置?喜欢在哪个项目(我的d2admin都有看法,我的partyweb有控制器和partybiz有型号).. – mmssaann

+0

非常好的问题。它必须与将模型绑定到数据库的部分相关,在您的案例中,这将是partybiz。我可能是错的,但那是我第一次尝试的地方。 – Ohlin

+0

嗯,谢谢。我已经尝试过活页夹,它工作的很棒!最后一个问题,是否有可能通用绑定到所有模型?我有超过20,30个模型,每个模型都有粘合剂并不好,不是吗? – mmssaann