C# 我错过了什么?(.NET、Razor、MVC)
我有一个来自主ViewModel的部分ViewModel 我需要用Dictionary方法返回dropdownlist状态模型的字节C# 我错过了什么?(.NET、Razor、MVC),c#,.net,razor,asp.net-mvc-5,C#,.net,Razor,Asp.net Mvc 5,我有一个来自主ViewModel的部分ViewModel 我需要用Dictionary方法返回dropdownlist状态模型的字节 public static class ListStatus { public static Byte Rejected = 0; public static Byte Pending = 1; public static Byte Reviewed = 2; public static Byte Accepted = 3; }
public static class ListStatus
{
public static Byte Rejected = 0;
public static Byte Pending = 1;
public static Byte Reviewed = 2;
public static Byte Accepted = 3;
}
局部视图模型:
@model byte
@{
Layout = null;
CategoryBusiness Business = new CategoryBusiness();
object attr = this.ViewBag.attr ?? new { @class = "form-control" };
RouteValueDictionary attrDictionary = (attr as RouteValueDictionary) ?? new RouteValueDictionary(attr);
attrDictionary["class"] = "required form-control";
Dictionary<int, string> statuses = new Dictionary<int, string>();
statuses.Add(Category.ListStatus.Rejected, "Rejected");
statuses.Add(Category.ListStatus.Pending, "Pending");
statuses.Add(Category.ListStatus.Reviewed, "Reviewed");
statuses.Add(Category.ListStatus.Accepted, "Accepted");
<div class="form-group">
@Html.LabelFor(model => model, new { @class = "col-sm-2 control-label text-right" })
<div class="col-sm-10 editor-field">
@Html.DropDownListFor(model => model, statuses.Select(x => new SelectListItem
{
Text = x.Value,
Value = x.Key.ToString(),
Selected = Convert.ToByte(x.Value) == Model
}))
@if (ViewData.ModelState.ContainsKey(Html.NameForModel().ToString()) && ViewData.ModelState[Html.NameForModel().ToString()].Errors.Count > 0)
{
@Html.Raw(Html.ValidationMessageFor(model => model, null, htmlAttributes: new { @class = "error" }).ToHtmlString().Replace("span", "label"))
}
</div>
</div>
@型号字节
@{
布局=空;
类别业务=新类别业务();
object attr=this.ViewBag.attr??新建{@class=“form control”};
RouteValueDictionary attrDictionary=(attr作为RouteValueDictionary)??新的RouteValueDictionary(attr);
attrDictionary[“类”]=“必需的表单控件”;
字典状态=新建字典();
状态。添加(Category.ListStatus.Rejected,“Rejected”);
status.Add(Category.ListStatus.Pending,“Pending”);
状态。添加(Category.ListStatus.revieved,“revieved”);
status.Add(Category.ListStatus.Accepted,“Accepted”);
@LabelFor(model=>model,新的{@class=“col-sm-2控件标签文本右侧”})
@DropDownListFor(model=>model,status.Select(x=>newselectListItem
{
Text=x.值,
Value=x.Key.ToString(),
所选=转换为ToByte(x.Value)=模型
}))
@if(ViewData.ModelState.ContainsKey(Html.NameForModel().ToString())和&ViewData.ModelState[Html.NameForModel().ToString()]错误数>0)
{
@Html.Raw(Html.ValidationMessageFor(model=>model,null,htmlAttributes:new{@class=“error”}).ToHtmlString().Replace(“span”,“label”))
}
我在DropDownList for中有一个错误,只有一个例外
“输入字符串的格式不正确。”不应该
选择=转换.ToByte(x.Value)==Model
实际上是选择=转换.ToByte(x.Key.ToString())==Model
?以下行有问题-
Selected = Convert.ToByte(x.Value) == Model
根据字典,x值是字符串值。可以是字符串,但不能转换为有效数字。这就是为什么它不能转换为字节,并引发FormatException
要了解更多关于Convert.ToByte的信息,您可以通过
解决方案是如下更改代码
Selected = Convert.ToByte(x.Key) == Model
MVC中的ViewModel?真的吗?你遗漏了一个问题。你的问题到底是什么?@JeremyWeir我在DropDownlistFor中有一个错误,异常是“输入字符串的格式不正确”。@S.Serp我在DropDownlistFor中有一个错误,异常是“输入字符串的格式不正确”我不知道发生了什么!但它确实有效!谢谢!这是毫无意义的-只需删除那行代码。使用
DropDownListFor()
方法时,Selected
属性被完全忽略