0

我有一个3实体的观点。 当我点击提交按钮时,我想保存这些实体。我想一次保存3个实体条目。如何做到这一点

在视图

<div class="editor-field "> 
    <%: Html.TextBoxFor(model => model.ElementAt(0).UserQuestion1)%> 
    <%: Html.ValidationMessageFor(model => model.ElementAt(0).UserQuestion1)%> 
</div> 
<div class="editor-field "> 
    <%: Html.TextBoxFor(model => model.ElementAt(0).UserAnswer)%> 
    <%: Html.ValidationMessageFor(model => model.ElementAt(0).UserAnswer)%> 
</div> 

<div class="editor-field "> 
    <%: Html.TextBoxFor(model => model.ElementAt(1).UserQuestion1)%> 
    <%: Html.ValidationMessageFor(model => model.ElementAt(1).UserQuestion1)%> 
</div> 
<div class="editor-field "> 
    <%: Html.TextBoxFor(model => model.ElementAt(1).UserAnswer)%> 
    <%: Html.ValidationMessageFor(model => model.ElementAt(1).UserAnswer)%> 
    </div> 

    <div class="editor-field "> 
    <%: Html.TextBoxFor(model => model.ElementAt(2).UserQuestion1)%> 
    <%: Html.ValidationMessageFor(model => model.ElementAt(2).UserQuestion1)%> 
    </div> 
    <div class="editor-field "> 
    <%: Html.TextBoxFor(model => model.ElementAt(2).UserAnswer)%> 
    <%: Html.ValidationMessageFor(model => model.ElementAt(2).UserAnswer)%> 
    </div> 

在控制器

public ActionResult ChooseQuestion() 
    { 
     List<UserQuestion> lst = new List<UserQuestion>() { 
      new UserQuestion(),new UserQuestion(), new UserQuestion() 
     }; 
     return View(lst); 
    } 

    [HttpPost] 
    public void ChooseQuestion(List<UserQuestion> lst) 
    { 
     //lst is always NULL Why 
     //EntityFactory.GetEntity().SaveChanges(); 
    } 

为什么当我点击提交按钮,我的参数列表LST为null。 我想进行保存。

谢谢。

回答

0

不是元素at,而是尝试使用模型[0]语法。这用在我见过的例子中。

HTH。

+0

这是我所需要的。谢谢。 – 2010-11-03 15:19:51

0

如果您有已知数量的实体,则可以为每个实体构造一个具有不同名称的自定义视图模型。当你提交它们时,它们应该被正确绑定。

+0

我不知道实体的数量,我在这个例子中使用3。谢谢你的帮助。 – 2010-11-02 01:26:05

相关问题