开发者

How to Get Model Data from Partial View?

I am creating a site in which I utilize partial views to display various bits of data about a single Model. Here is a bit of the HTML. (Note, all of these are contained within a single form and the Index page that these partials are rendered in is strongly typed to the main model. The main model contains various lists of data.)

<div id="tab1"><% Html.RenderPartial("Tab1", Model); %></div>
<div id="tab2"><% Html.RenderPartial("Tab2", Model.AnItemList1.FirstOrDefault<AnItemList1>()); %></div>
<div id="tab3"><% Html.RenderPartial("Tab3", Model.AnItemList2.FirstOrDefault()); %></div>

Here is ONE of the partial views headers (for 'tab2'):

<%@ Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AnItem1>" %>

The pages display correctly. The issue is that, when I enter data into the various parts of the partial pages and then submit the entire form (via POST), the data is not making it back to my data store (MSSQL) - but this only happens for any of the list items (that are contained within the Model). The first partial page does properly have its data set within the data store.

What am I doing wrong here? Should I only be passing the model to Html.RenderPartial and then get the specific model I need on the partial page? Should I pass the entire list and then get the first (right now, I only care about the first item in the list - that will EVENTUALLY change, but not any time soon).

Suggestions or thoughts appreciated.

Update: Here is how I accessing the properties on the partial views.

<div class="data-group">
    <%: Html.CheckBoxFor(model => model.Property1) %>
    <%: Html.LabelFor(model => model.Property1) %>
</div> 

Update 2: Per request...

Controller Action (ScenarioController):

    public ActionResult Index(int id = 0)
    {
        if (id == 0)
        {
            SavedScenario scenario = new SavedScenario();
            scenario.AnItemList1.Add(new AnItem1());
            scenario.AnItemList2.Add(new AnItem2());
            return View("Index", scenario);
        }
        else
        {
            SavedScenario scenario = repository.GetScenario(id);

            if (scenario == null)
                return View("NotFound");
            else
                return View("Index", scenario);
        }
    }

    [HttpPost]
    public ActionResult Index(SavedScenario scenario)
    {
        if (ModelState.IsValid && TryUpdateModel(scenario, "SaveScenario"))
        {
            repository.Add(scenario);
            repository.Save();
        }

        return View(scenario);
    }

Rendered HTML (I can only include parts of it - this is a small sample of what is in the form):

<form action="/Scenario" id="form0" method="post">
    <!-- This is the one that works - the basic Scenario. Top level. -->
  <fieldset>
    <legend>Scenario Information</legend>

    <div class="data-group">
        <div class="editor-label">
            <label for="ScenarioName">Scenario Name</label>

        </div>
        <div class="option1">
            <input class="wide" id="ScenarioName" name="ScenarioName" type="text" value="" />
        </div>
        <div class="validation">
            <div><span class="field-validation-valid" id="ScenarioName_validationM开发者_StackOverflow社区essage"></span></div>
        </div>
    </div>
  </fieldset>   

<!-- This does not work or get submitted (as far as I can tell). -->
<div id="tab2">
<fieldset>
    <legend>Tab2</legend>
    <div class="data-group">
        <input id="Property1" name="Property1" type="checkbox" value="true" /><input name="Property1" type="hidden" value="false" />
        <label for="Property1" />
    </div> 
</div>
</fieldset>

</form>

My apologies for having to keep this so generic.


Hard to guess from this much code. However you should make sure that all properties of your models have the same prefix when they are posted back to the server

Edit: form field names should match property names of your model to correctly bind all values. You have two fields with the same name that you can bind in following way

[HttpPost]
    public ActionResult Index(SavedScenario scenario, List<bool> Property1)
    {
        // here you can do with values coming in property1
        if (ModelState.IsValid && TryUpdateModel(scenario, "SaveScenario"))
        {
            repository.Add(scenario);
            repository.Save();
        }

        return View(scenario);
    }


It might be issue with naming the fields on your partial forms. Try naming the fields on your partial views by prefixing it with the name of the Model passed into it...like 'AnItemList1.name' instead of just 'name'..I am just guessing here though...but that's what I did sometimes to fix the problem when I was getting values as null..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜