简体   繁体   中英

properties of View Model are not set on HttpPost

My View Model code:

public class Step2ViewModel : MultiStepBaseViewModel
{
    public IList<LayoutDetail> LayoutConfig { get; set; }
}

My View code:

@model eliteemail.Web.Mvc.Areas.Emails.ViewModels.Step2ViewModel
@{
    ViewBag.Title = "Layout";
    Layout = "~/Views/Shared/_HomeLayout.cshtml";
}

@using (Html.BeginForm())
{

    @Html.ValidationSummary(true)

    for (int i = 0; i < @Model.LayoutConfig.Count(); i++)
    {
    <div class="grid_3 tcenter">
        <div class="divhighlight">
            <div style="width: 165px; margin: 6px auto 4px auto;" class="f16 bold tcenter" id="helptrigger1">@Model.LayoutConfig[i].LayoutTitle</div>
            <a class="fancybox" rel="layouts" href="@Model.LayoutConfig[i].LayoutImgPrev" title="1 Column">
                <img src="@Model.LayoutConfig[i].LayoutImg" alt="1 Column" width="189" height="227" vspace="5" />
            </a>
            <div style="width:189px; margin:auto">
                <input type="submit" class="button gobutton" style="margin-right: 40px; width: 165px;" value="Select" name="@Model.LayoutConfig[i].LayoutID,@Model.LayoutConfig[i].LayoutTitle"/>
            </div>
        </div>
    </div>
    } 
}

My Controller code:

public ActionResult Step2()
{
    Step2BusinessLogic step2BusinessLogic = new Step2BusinessLogic();
    Step2ViewModel step2ViewModel = step2BusinessLogic.CreateStep2ViewModel();
    return View(step2ViewModel);
}

[HttpPost]
public ActionResult Step2(Step2ViewModel step2ViewModel)
{
    ....
}

My Business Logic Class code:

public class Step2BusinessLogic
{
    public Step2ViewModel CreateStep2ViewModel(string Id)
    {
        Step2ViewModel step2ViewModel = new Step2ViewModel();
        step2ViewModel.MultiStepId = new Guid(Id);
        step2ViewModel.LayoutConfig = GetLayout();
        return createEmailStep2ViewModel;
    }

    public List<LayoutDetail> GetLayout()
    {
        List<LayoutDetail> layoutList = new List<LayoutDetail>();

        LayoutDetail layout1 = new LayoutDetail();
        layout1.LayoutID = 1;
        layout1.LayoutTitle = "1 Column";
        layout1.LayoutImg = "~/img/create/layout/layout-1.png";
        layout1.LayoutImgPrev = "img/create/layout/layout-1-preview.png";
        layoutList.Add(layout1);

        LayoutDetail layout2 = new LayoutDetail();
        layout2.LayoutID = 2;
        layout2.LayoutTitle = "1:2 Column";
        layout2.LayoutImg = "~/img/create/layout/layout-2.png";
        layout2.LayoutImgPrev = "img/create/layout/layout-2-preview.png";
        layoutList.Add(layout2);

        .........(12 Items)

        return layoutList;
    }
}

public class LayoutDetail
{
    public int LayoutID { get; set; }

    public string LayoutTitle { get; set; }

    public string LayoutImg { get; set; }

    public string LayoutImgPrev { get; set; }
}

The problem is that when I submit from Step2View.cshtml , then

public ActionResult Step2(Step2ViewModel step2ViewModel)

is being called but the properties of step2ViewModel are not being set and ModelState.IsValid is returning false. I want to avoid both conditions. Any help please, as I am very new to MVC.

I searched several links but can't understand the scenario.

The problem is that you are not submitting anything when you do the post. You have no form elements other than a button. If you had form elements, then the data would be passed back. Try adding an @Html.HiddenFor(o=>o.LayoutID) in your view and then posting again. The form will post back hidden elements or form fields, but not display only fields as you are doing.

Do not use a foreach loop on a collection object as the model binder cannot work with it. Use a for-loop instead. In your code replace each occurence of @item with @Model.LayoutConfig[i] , see updated code below:

for(var i=0;i<@Model.LayoutConfig.Count();i++)
{
    <div class="grid_3 tcenter">
        <div class="divhighlight">
            <div style="width: 165px; margin: 6px auto 4px auto;" class="f16 bold tcenter" id="helptrigger1">@Model.LayoutConfig[i].LayoutTitle</div>
            <a class="fancybox" rel="layouts" href="@Model.LayoutConfig[i].LayoutImgPrev" title="1 Column">
                <img src=@Model.LayoutConfig[i].LayoutImg" alt="1 Column" width="189" height="227" vspace="5" />
            </a>
            <div style="width:189px; margin:auto">
                <input type="submit" class="button gobutton" style="margin-right: 40px; width: 165px;" value="Select" name="@Model.LayoutConfig[i].LayoutID, @Model.LayoutConfig[i].LayoutTitle"/>
            </div>
        </div>
    </div>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM