开发者

Add object to array in an MVC model

If I have a strongly typed view with a model that has an array of another object, is it possible to have a strongly typed partial view that adds that other object to the model?

For example, if I had a view typed HandSurvey, which has an array of CurrentGlove, could I have a partial view in the hand survey form strongly typed to CurrentGlove that when the user hits the submit button, it doe开发者_运维知识库sn't return a new view, but adds a CurrentGlove object to the HandSurvey model's array? How would I go about that? Sorry if that didn't make sense, I am having a lot of trouble getting a grasp on the mvc structure.

These are the models:

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; }
}

this is the code in the view, typed to 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>

this is the code in the partial view, typed to CurrentGlove:

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


Try checking this guide. I think it might help.


I'm more of a MVC3 guy so this might be a guess.

Take the form out of your partial view for CurrentGlove.

Move the form in your first view to encompass the partial view for CurrentGlove.ascx

And when calling the partial view, do it like this:

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

Now when the form posts, it will pass in all the form data into the action at the same time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜