1

我的视图模型代码:

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

我的查看代码:

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

我的控制器代码:

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

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

我的业务逻辑类代码:

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

问题是当我从 提交时Step2View.cshtml,然后

public ActionResult Step2(Step2ViewModel step2ViewModel)

正在调用但未设置的属性step2ViewModelModelState.IsValid返回 false。我想避免这两种情况。请提供任何帮助,因为我对 MVC 很陌生。

我搜索了几个链接,但无法理解场景。

4

2 回答 2

2

问题是您在发帖时没有提交任何内容。除了按钮之外,您没有任何表单元素。如果您有表单元素,那么数据将被传回。尝试 @Html.HiddenFor(o=>o.LayoutID)在您的视图中添加一个,然后再次发布。表单将回发隐藏元素或表单字段,但不会像您那样只显示字段。

于 2013-04-23T08:53:26.963 回答
0

不要foreach在集合对象上使用循环,因为模型绑定器无法使用它。改用 a for-loop。在您的代码中,将每次出现的替换为@item@Model.LayoutConfig[i]请参阅下面的更新代码:

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>
}
于 2013-04-23T08:52:42.103 回答