2011-08-11 26 views
0

如果我有一個具有另一個對象數組的模型的強類型視圖,是否可以使用強類型局部視圖將該其他對象添加到模型中?將對象添加到MVC模型中的數組

例如,如果我有一個視圖類型HandSurvey,其具有CurrentGlove陣列,我可以在強類型到CurrentGlove,當用戶點擊提交按鈕,它沒有手調查表格的局部視圖返回一個新的視圖,但是將CurrentGlove對象添加到HandSurvey模型的數組中?我會怎麼做呢?對不起,如果沒有任何意義,我在掌握mvc結構方面遇到很多麻煩。

這些是模型:

public class HandSurveyModel 
{ 
    public HandSurveyModel() { } 

    [DisplayName("Location Number:")] 
    public string LocationNumber { get; set; } 
    [DisplayName("Location Name:")] 
    public string LocationName { get; set; } 
    public List<HandSurveyRisk> Risks { get; set; } 
    public List<CurrentGlove> CurrentGloves { get; set; } 
} 

public class CurrentGlove 
{ 
    public CurrentGlove() { } 

    public int Quantity { get; set; } 
    public string MFGNumber { get; set; } 
    public string Size { get; set; } 
    public string UOM { get; set; } 
    public string Description { get; set; } 
    public string Department { get; set; } 
    public string Comments { get; set; } 
} 

這是在視圖的代碼,輸入到HandSurveyModel

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <fieldset> 
     <legend>Hand Protection Survey</legend> 
     <% using (Html.BeginForm("HandSurvey", "Resources")) 
      { %> 
     <div style="float: left; margin-left: 10px; margin-right: 10px"> 
      <%= Html.LabelFor(x => Model.LocationNumber)%> 
      <%= Html.TextBoxFor(x => Model.LocationNumber) %></div> 
     <div> 
      <%= Html.LabelFor(x => Model.LocationName)%> 
      <%= Html.TextBoxFor(x => Model.LocationName) %></div> 
     <fieldset> 
      <legend>Risk Assessment</legend> 
      <fieldset style="float: left; margin: 10px"> 
       <legend>Chemical</legend> 
       <% for (int i = 0; i < Model.Risks.Count; i++) 
        {%> 
       <%= Html.CheckBoxFor(x => x.Risks[i].IsChecked)%> 
       <%= Html.DisplayFor(x => x.Risks[i].Text)%> 
       <br /> 
       <%} %> 
      </fieldset> 
       <input type="submit" value="OK" /> 
      <% } %> 
     </fieldset> 
     <fieldset> 
      <legend>Current Gloves</legend> 
      <% Html.RenderPartial("~/Views/Resources/CurrentGlove.ascx", new CurrentGlove()); %> 
     </fieldset> 
    </fieldset> 
</asp:Content> 

這是在局部視圖的代碼,輸入到CurrentGlove

<% using (Html.BeginForm("CurrentGlove", "Resources")) { %> 
    <%= Html.EditorForModel() %> 
    <p><input type="submit" value="OK" id="btnSearch"/></p> 
<% } %> 
+0

請發佈您的代碼到目前爲止。另外我覺得你幾天前已經問過這個問題了? – Yuck

+0

@Yuck,代碼添加。你是正確的,我真的問過如何更新基於這個表單的數據網格。但現在我意識到,我甚至不知道如何達到這一點...... – Nick

回答

0

我更多的是一個MVC3傢伙,所以這可能是一個猜測。

從的局部視圖中取出表格。

移動形式,你的第一個視圖包含用於CurrentGlove.ascx

並調用局部視圖時,局部視圖,像這樣做:現在

<% Html.RenderPartial("~/Views/Resources/CurrentGlove.ascx", Model.CurrentGloves); %> 

當表單的帖子,它會通過將所有表單數據同時轉化爲行動。

+0

我需要允許用戶在Hand Survey上爲模型添加多個'CurrentGlove',一旦完成,提交手調查作爲一個整體。那麼他們如何能夠爲一個'CurrentGlove'輸入信息,然後點擊一個按鈕將它添加到'HandSurvey'模型中的數組中? – Nick

1

嘗試檢查this指南。我認爲這可能會有所幫助。