gpt4 book ai didi

asp.net-mvc - MVC 4,复选框列表和我

转载 作者:行者123 更新时间:2023-12-02 04:10:05 26 4
gpt4 key购买 nike

大家早上好。

我可以看到这已经在其他地方讨论过,但想知道 MVC 4 中是否有任何改变或者对于像我这样的傻瓜来说事情变得更简单?!

场景

我有以下经过编辑的模型:

public class CorporateDetails
{

public Guid? Id { get; set; }

[Key]
public int CorporateDetailId { get; set; }

public int? EmsId { get; set; }
public string EmsName { get; set; }

public virtual EmsType EmsType { get; set; }
}

public class EmsType
{
[Key]
public int? EmsId { get; set; }
public string EmsName { get; set; }

public virtual ICollection<EmsType> EmsTypes { get; set; }
}

使用以下标准创建 View :

 <fieldset>
<legend>CorporateDetails</legend>



<div class="editor-label">
@Html.LabelFor(model => model.EmsId, "EmsType")
</div>
<div class="editor-field">
@Html.DropDownList("EmsId", String.Empty)
@Html.ValidationMessageFor(model => model.EmsId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmsName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmsName)
@Html.ValidationMessageFor(model => model.EmsName)
</div>

<p>
<input type="submit" value="Create" />
</p>
</fieldset>

这给了我一个开箱即用的漂亮的下拉列表 Scott Gu's blog

现在我真正的问题是 - 如何有效地将这个下拉框转换为有效的多选、复选框列表?

再次,对于踩过的地方表示歉意,但我只是在测试水,看看是否发生了任何更新。

请注意,第一个 MVC 项目,所以慢慢来,我又感觉很厚了:'(

最佳答案

好的,我已经解决了 - 万岁!正如您从评论中看到的,出现了一些问题,但请在下面找到有效的完整解决方案:D

型号

 public class CorporateDetails
{

public Guid? Id { get; set; }

[Key]
public int CorporateDetailId { get; set; }

public int[] EmsId { get; set; }

}

public class EmsType
{
[Key]
public int EmsId { get; set; }
public string EmsName { get; set; }

public virtual ICollection<EmsType> EmsTypes { get; set; }
}

Controller

 public ActionResult Create()
{
CorporateDetails corporatedetails = new CorporateDetails();
ViewBag.EmsId = new MultiSelectList(db.EmsTypes, "EmsId", "EmsName");
return View(corporatedetails);
}

扩展(放置在项目根目录的文件夹中)

 public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty[]>> expression, MultiSelectList multiSelectList, object htmlAttributes = null)
{
//Derive property name for checkbox name
MemberExpression body = expression.Body as MemberExpression;
string propertyName = body.Member.Name;

//Get currently select values from the ViewData model
TProperty[] list = expression.Compile().Invoke(htmlHelper.ViewData.Model);

//Convert selected value list to a List<string> for easy manipulation
List<string> selectedValues = new List<string>();

if (list != null)
{
selectedValues = new List<TProperty>(list).ConvertAll<string>(delegate(TProperty i) { return i.ToString(); });
}

//Create div
TagBuilder divTag = new TagBuilder("div");
divTag.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);

//Add checkboxes
foreach (SelectListItem item in multiSelectList)
{
divTag.InnerHtml += String.Format("<div><input type=\"checkbox\" name=\"{0}\" id=\"{0}_{1}\" " +
"value=\"{1}\" {2} /><label for=\"{0}_{1}\">{3}</label></div>",
propertyName,
item.Value,
selectedValues.Contains(item.Value) ? "checked=\"checked\"" : "",
item.Text);
}

return MvcHtmlString.Create(divTag.ToString());
}

在 View 的 Web 配置中注册扩展

 <pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="MyProject.Extensions" />
</namespaces>
</pages>

查看

@model Valpak.Websites.HealthChecker.Models.CorporateDetails
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)

<fieldset>
<legend>CorporateDetails</legend>

<div class="editor-label">
@Html.CheckBoxListFor(model => model.EmsId, (MultiSelectList) ViewBag.EmsId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

这给了我一个可爱的复选框列表。万岁!

感谢 Darin 的帮助,我已将此标记为答案,但为您的时间和精力 +50。

关于asp.net-mvc - MVC 4,复选框列表和我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11204464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com