在我的 ASP.Net MVC3 Razor 项目中,我必须实现一个客户注册表单(Screen Shot Attached)。在这种形式中,单个实体(比如:做DMIt的目的)包含多个所以我使用复选框来选择多个或单个答案。我有视图页面和模型。如何编写视图页面以选择多个复选框以及控制器。
控制器代码
public ActionResult CustomerRegistration()
{
return View();
}
型号代码
namespace Elixir.Models
{
[Table("tbl_ElixirCustomer")]
public class Customer
{
[Key]
public int CusId { get; set; }
public string Name { get; set; }
public int age { get; set; }
public int Gender { get; set; }
public string FathName { get; set; }
public string MothName { get; set; }
public string OrgSchooName { get; set; }
public string Address { get; set; }
public string city { get; set; }
public string State { get; set; }
public string PIN { get; set; }
public string tele { get; set; }
public string Mob { get; set; }
public string Email { get; set; }
public string Web { get; set; }
public string Purpose { get; set; }
public string brief { get; set; }
}
public class CustomerViewModel
{
public string Purpose { get; set; }
public int Id { get; set; }
public bool IsChecked { get; set; }
}
}
查看代码
<div class="col-lg-10">@Html.TextBoxFor(Model => Model.Mob, new { @class = "form-control" })</div>
<label class="col-lg-2 control-label">
Email</label>
<div class="col-lg-10">@Html.TextBoxFor(Model => Model.Email, new { @class = "form-control" })</div>
<label class="col-lg-2 control-label">
Web Site</label>
<div class="col-lg-10">@Html.TextBoxFor(Model => Model.Web, new { @class = "form-control" })</div>
<label class="col-lg-2 control-label">
Purpose of doing DMIT</label>
<div class="col-lg-10">
<div class="styled-chekbox">
<input type="checkbox" checked class="icheck-box-flat">
</div>
<span class="checkbox-label">Career Planning</span>
<div class="styled-chekbox">
<input type="checkbox" checked class="icheck-box-flat">
</div>
<span class="checkbox-label">Personel</span>
<div class="styled-chekbox">
<input type="checkbox" checked class="icheck-box-flat">
</div>
<span class="checkbox-label">Relationship</span>
<div class="styled-chekbox">
<input type="checkbox" checked class="icheck-box-flat">
</div>
<span class="checkbox-label">Parenting</span>
<div class="styled-chekbox">
<input type="checkbox" checked class="icheck-box-flat">
</div>
<span class="checkbox-label">Activity Plan for children</span>
<div class="styled-chekbox">
<input type="checkbox" checked class="icheck-box-flat">
</div>
<span class="checkbox-label">Stress Management</span>
</div>
<label class="col-lg-2 control-label">
Any Challenges</label>
<div class="col-lg-10">@Html.TextAreaFor(model => model.brief, new { @class = "tinymce-simple span12", @row = "170", @cols = "45", @width = "40%" })</div>
<div class="col-lg-2 control-label"></div>
<div class="col-lg-10">
@*<input type="button" class="" />*@ @* <button type="submit" class = "btn btn-success">@Html.ActionLink("Save", "EmployeeRegistration", "Home")</button>*@
@* <button type="submit" >@Html.ActionLink("Save", "EmployeeRegistration", "Home", new { @class = "btn btn-success" })</button>*@
<input type="submit" class="btn btn-success" value="Save" />
<button class="btn btn-success">
Clear</button>
<button class="btn btn-success">
Cancel</button>
</div>
答案 0 :(得分:3)
在模型中创建一个布尔属性
型号:
public String Question{ get; set; }
public Boolean Options{ get; set; }
public String OptionContent{ get; set; }
...so on
将此模型传递到视图中,然后使用EditorFor html helper。
@using (Html.BeginForm("actionname", "Home", FormMethod.Post, null)){
<div>
@Html.LabelFor(model => model.Question)
</div>
<div>
@Html.EditorFor(model => model.Option)
@Html.LabelFor(model => model.OptionContent)
</div>
}
答案 1 :(得分:3)
在CustomerViewModel
中,您可以为每个选项单独properties
public bool CareerPlanning { get; set; }
public bool Personal{ get; set; }
public bool RelationShip{ get; set; }
and So on.....
然后在视图中,您可以拥有这些属性的字段
@Html.CheckBoxFor(Model => Model.CareerPlanning )<span> Career Planning </span>
@Html.CheckBoxFor(Model => Model.Personal)<span> Personal </span>
@Html.CheckBoxFor(Model => Model.RelationShip) <span> RelationShip</span>
and So on.....
现在在控制器中,您需要根据所有复选框值修改Purpose
StringBuilder sb=new StringBuilder();
if(model.CareerPlanning)
sb.Append("Carrer Planning");
if(model.Personal)
sb.Append("-Personal");
and so on....
并在最后
model.Purpose=sb.ToString();