asp.net – 如何使用MVC 4和View Models(强类型)渲染一组复选框

前端之家收集整理的这篇文章主要介绍了asp.net – 如何使用MVC 4和View Models(强类型)渲染一组复选框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我更喜欢ASP.net MVC世界,而我正在试图找出如何渲染一组强大类型的视图模式的复选框.在webforms中,我将使用checkBoxlist控件,但是使用MVC有点丢失.

我正在为婚礼策划业务构建一个简单的联系表单,需要将用户选择的任何复选框值传递给我的控制器.

表单复选框需要如下所示:

您的帮助将不胜感激.谢谢!

这是我到目前为止.

CONTROLLER

[HttpPost]
public ActionResult Contact(Contactviewmodel ContactVM)
{
    if (!ModelState.IsValid)
    {
        return View(ContactVM);
    }
    else
    {
        //Send email logic

        return RedirectToAction("ContactConfirm");
    }
}

查看模型

public class Contactviewmodel
{
    [required]
    public string Name { get; set; }

    [required]
    public string Phone { get; set; }

    [required]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [required]
    public string Subject { get; set; }
    public IEnumerable<SelectListItem> SubjectValues
    {
        get
        {
            return new[]
            {
                new SelectListItem { Value = "General Inquiry",Text = "General Inquiry" },new SelectListItem { Value = "Full Wedding Package",Text = "Full Wedding Package" },new SelectListItem { Value = "Day of Wedding",Text = "Day of Wedding" },new SelectListItem { Value = "Hourly Consultation",Text = "Hourly Consultation" }  
            };
        }
    }


    //Not sure what I should do for checkBoxes...

}

视图

@model NBP.viewmodels.Contactviewmodel

@{
    ViewBag.Title = "Contact";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm())
{
    <div id="ContactContainer">
        <div><span class="requiredField">*&nbsp;</span>Your Name:</div>
        <div>
            @Html.TextBoxFor(model => model.Name)
        </div>
        <div><span class="requiredField">*&nbsp;</span>Your Phone:</div>
        <div>
            @Html.TextBoxFor(model => model.Phone)
        </div>
        <div><span class="requiredField">*&nbsp;</span>Your Email:</div>
        <div>
            @Html.TextBoxFor(model => model.Email)
        </div>
        <div>Subject:</div>
        <div> 
            @Html.DropDownListFor(model => model.Subject,Model.SubjectValues)
        </div>
        <div>Vendor Assistance:</div>
        <div>

            <!-- CHECKBoxES HERE -->

        </div>
        <div>
            <input id="btnSubmit" type="submit" value="Submit" />
        </div>
    </div>
}

解决方法

您可以丰富您的视图模型:
public class VendorAssistanceviewmodel
{
    public string Name { get; set; }
    public bool Checked { get; set; }
}

public class Contactviewmodel
{
    public Contactviewmodel()
    {
        VendorAssistances = new[]
        {
            new VendorAssistanceviewmodel { Name = "DJ/BAND" },new VendorAssistanceviewmodel { Name = "Officiant" },new VendorAssistanceviewmodel { Name = "Florist" },new VendorAssistanceviewmodel { Name = "Photographer" },new VendorAssistanceviewmodel { Name = "Videographer" },new VendorAssistanceviewmodel { Name = "Transportation" },}.ToList();
    }

    [required]
    public string Name { get; set; }

    [required]
    public string Phone { get; set; }

    [required]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [required]
    public string Subject { get; set; }
    public IEnumerable<SelectListItem> SubjectValues
    {
        get
        {
            return new[]
            {
                new SelectListItem { Value = "General Inquiry",Text = "Hourly Consultation" }  
            };
        }
    }

    public IList<VendorAssistanceviewmodel> VendorAssistances { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new Contactviewmodel());
    }

    [HttpPost]
    public ActionResult Index(Contactviewmodel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        //Send email logic
        return RedirectToAction("ContactConfirm");
    }
}

视图:

@using (Html.BeginForm())
{
    <div id="ContactContainer">
        <div><span class="requiredField">*&nbsp;</span>Your Name:</div>
        <div>
            @Html.TextBoxFor(model => model.Name)
        </div>
        <div><span class="requiredField">*&nbsp;</span>Your Phone:</div>
        <div>
            @Html.TextBoxFor(model => model.Phone)
        </div>
        <div><span class="requiredField">*&nbsp;</span>Your Email:</div>
        <div>
            @Html.TextBoxFor(model => model.Email)
        </div>
        <div>Subject:</div>
        <div> 
            @Html.DropDownListFor(model => model.Subject,Model.SubjectValues)
        </div>
        <div>Vendor Assistance:</div>
        <div>
            @for (int i = 0; i < Model.VendorAssistances.Count; i++)
            {
                <div>
                    @Html.HiddenFor(x => x.VendorAssistances[i].Name)
                    @Html.CheckBoxFor(x => x.VendorAssistances[i].Checked)
                    @Html.LabelFor(x => x.VendorAssistances[i].Checked,Model.VendorAssistances[i].Name)
                </div>
            }
        </div>
        <div>
            <input id="btnSubmit" type="submit" value="Submit" />
        </div>
    </div>
}
原文链接:https://www.f2er.com/aspnet/250690.html

猜你在找的asp.Net相关文章