0

I have a huge problem that I'm extremely confused on. I'll give you a little background first. I have two models, one that contains the other like

public class AccountViewModel
{   
    public ChargeViewModel chargeViewModel { get; set; }
    other public values
}

Now I have a view, that references the partial view like so.

@model MyModels.AccountViewModel

[[Block_Header]]
@{ Html.RenderPartial("Header"); }
[[/Block_Header]]

[[ValidationSummary]]
@Html.ValidationSummary(false)      
[[/ValidationSummary]]

[[Block_ReloadOptions]]
    <div data-role="fieldcontain">
        <fieldset data-role="controlgroup" data-type="horizontal" class="amount-bar">
            <legend>Select an amount</legend>
            @{
                List<int> PresetValues = Main.Denominations.Split(',').Select(x => Convert.ToInt32(x.Trim())).ToList();    
            }       
            @if (PresetValues.Count > 0){
                int i = 1;
                foreach (int value in PresetValues) {                    
                    <input type="radio" name="amount" id="amount@(i)" value="@value" @(Model.chargeViewModel.ChargeAmount == value ? "checked" : "")>
                    <label for="amount@(i)">$@value</label>
                    i++;
                } 
            }           
            <label for="select-amount">Select Amount</label>
            <select name="select-amount" id="select-amount" class="select-amount" data-role="select">
                <option value="">Other</option>
                   @if (Model.chargeViewModel.OtherValues.Count > 0){                      
                        foreach (int value in Model.chargeViewModel.OtherValues)
                        {
                            <option value="@value" @(Model.chargeViewModel.ChargeAmount == value ? "selected" : "") >$@value</option>                           
                        }
                    }                   
            </select>
            @Html.HiddenFor(m => m.chargeViewModel.ChargeAmount, new { id="hiddenAmount", value="0" })
        </fieldset>
    </div>

Some more forms here that arent relevant and then my partial view at the bottom as follows

@Html.Partial("MobileModelView", Model.chargeViewModel)

Now my partial view looks something like

@model VirtualNext.Web.Models.ChargeViewModel
<div class="ui-block-a">
        <fieldset data-role="controlgroup" data-type="horizontal" class="field-margin validate expiry-select">
                <label for="select-expiry-month" class="ui-hidden-accessible">Month</label>
                @Html.DropDownListFor(m => m.ExpMonth, Model.ddlMonths, new { id = "select-expiry-month", @class= "validate"})
                <label for="select-expiry-year" class="ui-hidden-accessible">Year</label>
                @Html.DropDownListFor(m => m.ExpYear, Model.ddlYears, new { id = "select-expiry-year", @class= "validate validateExpiry validateYear" })
        </fieldset>
    </div>

So now I'm having a problem passing in the values of m.ExpMonth and m.ExpYear to my model in the controller. I have checked my Request.Form value and the values are populated, but not to the post method in my controller.

My controller looks like

public ActionResult MyAction(AccountViewModel model, ChargeViewModel chargeViewModel)

{}

Now I have all the data that I pass in directly through my main view, but nothing from the partial view is populating the model like I want it to. Now this is part of my mobile site I'm working on, and I actually have something extremely similar on my desktop side, so I'm really confused as to why it wont work. Any ideas/suggestions would be greatly appreciated.

4

2 回答 2

2

你有错误的绑定。控制器中不需要 2 个模型。您可以将子模型与主模型绑定,例如

局部视图

@model VirtualNext.Web.Models.ChargeViewModel
@{
    ViewData.TemplateInfo.HtmlFieldPrefix = "AccountViewModel"; //bind to main model
}

控制器有 1 个模型

public ActionResult MyAction(AccountViewModel model)

另外,我建议您通过 ViewData 传递前缀,以使您的视图更加灵活。

@model VirtualNext.Web.Models.ChargeViewModel
@{
    ViewData.TemplateInfo.HtmlFieldPrefix = ViewData["htmlprefix"];
}

使用此选项,您可以在许多控制器操作中使用此视图。只需传递前缀并将您的子模型绑定到您需要的任何模型。

于 2013-07-16T18:56:48.740 回答
1

将您的局部视图放在 Views/Shared/EditorTemplates 文件夹中。

然后,将视图中的 Html.Partial 行替换为:

@Html.EditorFor(model => model.ChargeViewModel, "MobileModelView")

并且,将您的 Action 替换为:

public ActionResult MyAction(AccountViewModel model)
{
  // Some code...
}
于 2013-07-16T18:59:11.987 回答