使用IList <t>绑定.cshtml中的值</t>

时间:2013-04-22 12:17:31

标签: asp.net-mvc list razor asp.net-mvc-4

我的控制器内容是:

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

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

Step2ViewModel有一个像......

这样的属性
public class Step2ViewModel : MultiStepBaseViewModel
{
    public IList<LayoutDetail> LayoutConfig { get; set; }
}

商业逻辑课就像......

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

在我的.cshtml视图中,我想要这样的东西......

@using (Html.BeginForm())
{
    @Html.HiddenFor(model => model.MultiStepId)
    @Html.ValidationSummary(true)

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

我想将Step2ViewModel的IList<LayoutDetail>属性的值绑定到.cshtml页面的控件。我尝试了其他几件事,但不能成功

2 个答案:

答案 0 :(得分:0)

您需要使用

@Model.LayoutConfig[i].LayoutImg

而不是

@LayoutConfig.LayoutImg

当然还有其他情况

@LayoutConfig.LayoutID
@LayoutConfig.LayoutImgPrev

答案 1 :(得分:0)

您遗失了这个@Model.LayoutConfig[i]

@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">
        <button class="button gobutton" style="margin-right: 40px; width: 165px;" value="@Model.LayoutConfig[i].LayoutID">Select</button></div>
        </div>
        </div>
    }