.net – 如何使用viewmodel绑定选择列表?
发布时间:2020-05-23 22:19:53 所属栏目:asp.Net 来源:互联网
导读:我无法获得一个绑定到我的ViewModel的选择列表. 我有一个ViewModel,它包含一个Question实体和一个字符串 public class QuestionViewModel{ public Question Question { get; set; } public string RefUrl { get; set; } public QuestionV
|
我无法获得一个绑定到我的ViewModel的选择列表. 我有一个ViewModel,它包含一个Question实体和一个字符串 public class QuestionViewModel
{
public Question Question { get; set; }
public string RefUrl { get; set; }
public QuestionViewModel()
{
}
public QuestionViewModel(Question question,string RefUrl)
{
this.Question = question;
this.RefUrl = RefUrl;
}
public QuestionViewModel(Question question)
{
this.Question = question;
this.RefUrl = "";
}
}
这是控制器: public ActionResult Edit(int id)
{
Question question = db.Question.Single(q => q.question_id == id);
QuestionViewModel qvm = new QuestionViewModel(question);
ViewBag.category_id = new SelectList(db.Category,"category_id","category_name",qvm.Question.category_id);
ViewBag.type_code = new SelectList(db.Question_Type,"type_code","type_description",qvm.Question.type_code);
return View(qvm);
}
我视图中的代码如下所示: <div class="editor-label">
@Html.LabelFor(model => model.Question.type_code,"Question_Type")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => Model.Question.Question_Type,(SelectList)ViewBag.type_code)
@Html.ValidationMessageFor(model => model.Question.type_code)
</div>
View确实将Question实体的Question_Type设置为所选值,但是当我提交表单时, 解决方法你拥有的不是一个视图模型.它是一个混合类,你称之为视图模型,并且你已经包装了你的域实体(问题).这很糟糕,不要这样做.这是我建议你的.首先设计一个真实的视图模型,它将反映您视图的要求(从您当前的描述中,它是一个包含一些问题类型的下拉列表,并允许用户从此ddl中选择一些问题类型): public class QuestionViewModel
{
[DisplayName("Question_Type")]
public string SelectedQuestionType { get; set; }
public IEnumerable<SelectListItem> QuestionTypes { get; set; }
// didn't see where you are using this on your view
public string RefUrl { get; set; }
}
然后让您的控制器映射到您的域模型和视图模型之间.当然,进一步的改进是使用AutoMapper来避免遍布控制器操作的这种映射: public ActionResult Edit(int id)
{
var question = db.Question.Single(q => q.question_id == id);
var qvm = new QuestionViewModel
{
// preselect a value
SelectedQuestionType = question.type_code,QuestionTypes = db.Question_Type.Select(x => new SelectListItem
{
Value = x.type_code,Text = x.type_description
})
};
return View(qvm);
}
接着: <div class="editor-label">
@Html.LabelFor(x => x.SelectedQuestionType)
</div>
<div class="editor-field">
@Html.DropDownListFor(
x => SelectedQuestionType,new SelectList(Model.QuestionTypes,"Value","Text")
)
@Html.ValidationMessageFor(x => x.SelectedQuestionType)
</div>
最后一句话:确保你已经摆脱了任何ViewBag / ViewData的丑陋,并将你的视图需要放到视图模型中.您已在控制器操作中显示了一些类别,这些类别未在您显示的视图片段中显示.如果您需要它们,只需将它们放在视图模型中,就像我们对问题类型所做的那样. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – HttpPostedFileBase.SaveAs方法问题
- asp.net – tinymce在回发时获取HTML代码
- asp.net – 线程被中止
- asp.net – Web Api – 如何直接从OnActionExecuting过滤器
- asp.net – 如何摆脱丑陋的asp:菜单闪烁?
- asp.net-mvc-3 – 对复选框不起作用的MVC不显眼的验证
- asp.net – Web API 2 – ApiController.InternalServerErr
- asp.net-mvc – 为什么Chrome在ASP.NET MVC中提供文件时搜索
- asp.net – 如何在visual studio中查看cshtml页面?
- asp.net-core – Namespace .AspNetCore.Hosting vs .Exten
推荐文章
站长推荐
- asp.net – 如何让Html Link(锚点)回发像LinkBut
- asp.net – HttpMessageHandler与DelegatingHand
- 什么时候最早我可以在ASP.NET MVC页面生命周期中
- asp.net-mvc – MVC的编码标准 – 它们是官方的吗
- asp.net-mvc – Nhibernate / MVC:在View中处理
- asp-classic – 如何检查VBScript中是否存在POST
- 如何修复ASP.NET错误“文件’nnn.aspx’没有预编
- ASP.NET云应用程序与普通的ASP.NET
- asp.net使用H5新特性实现异步上传的示例
- 有标签的ASP.NET WebControl吗?
热点阅读
