问题描述
在 MVC4 中:
我的模型中有以下属性用于下拉列表:
public SelectList Subjects { get;放;}
我在页面加载时在我的 Index() Action 中设置了 Subjects 属性并返回模型.
下拉列表被 SelectListItems 填充得很好.
@Html.DropDownListFor(x => x.Subject, new SelectList(Model.Subjects, "Text", "Text", "Other"))
当我提交表单时,模型中的 Subjects SelectList 已更改为 null.必须有一种简单的方法可以将其保留在 HttpPost 上.我假设我也想提交和发布这个 SelectList 以及所有表单字段?我该怎么做?
通常可以接受在 Post
操作之后重新填充 SelectList
.只需在方法中提取它并在 Get
和 Post
操作中调用它.
再次将其发布回控制器不是可行的方法.您可以缓存 SelectList 中的项目,这样您就不必对数据存储进行两次查询.
示例:
public ActionResult Create(){var model = new SubjectModel();填充主题列表(模型);返回视图(模型);}[HttpPost]公共 ActionResult 创建(主题模型模型){如果(模型状态.IsValid){//保存项目..}//出问题了.填充主题列表(模型);返回视图(模型);}private void PopulateSubjectList(SubjectModel 模型){if (MemoryCache.Default.Contains("SubjectList")){//缓存中已经存在 SubjectList,model.Subjects = (List)MemoryCache.Default.Get("SubjectList");}别的{//缓存中尚不存在选择列表,从数据存储中获取项目.列表selectList = _db.Subjects.ToList();//将列表缓存在内存中 15 分钟.MemoryCache.Default.Add("SubjectList", selectList, DateTime.Now.AddMinutes(15));model.Subjects = selectList;}}
注意:MemoryCache
使用 System.Runtime.Caching
命名空间.请参阅:System.Runtime.Caching 命名空间.>
此外,缓存应该位于控制器(或业务层)和数据访问层之间的单独层中,这只是为了清楚起见.
In MVC4:
I have the following property in my model used for a dropdown list:
public SelectList Subjects { get; set; }
I set the Subjects property in my Index() Action on page load and return the model.
The dropdown gets populated just fine with the SelectListItems.
@Html.DropDownListFor(x => x.Subject, new SelectList(Model.Subjects, "Text", "Text", "Other"))
When I submit the form the Subjects SelectList in the model has changed to null. There has to be a simple way to persist this on HttpPost. I assume I want to submit and post this SelectList as well, along with all the form fields? How would I do this?
It is commonly accepted that you re-populate a SelectList
after the Post
action. Just extract it inside a method and call it in the Get
and Post
action.
Posting it back again to the controller is not the way to go. You can cache the items in the SelectList so you won't have to make a query to the data store twice.
Example:
public ActionResult Create()
{
var model = new SubjectModel();
PopulateSubjectList(model);
return View(model);
}
[HttpPost]
public ActionResult Create(SubjectModel model)
{
if (ModelState.IsValid)
{
// Save item..
}
// Something went wrong.
PopulateSubjectList(model);
return View(model);
}
private void PopulateSubjectList(SubjectModel model)
{
if (MemoryCache.Default.Contains("SubjectList"))
{
// The SubjectList already exists in the cache,
model.Subjects = (List<Subject>)MemoryCache.Default.Get("SubjectList");
}
else
{
// The select list does not yet exists in the cache, fetch items from the data store.
List<Subject> selectList = _db.Subjects.ToList();
// Cache the list in memory for 15 minutes.
MemoryCache.Default.Add("SubjectList", selectList, DateTime.Now.AddMinutes(15));
model.Subjects = selectList;
}
}
Note: MemoryCache
uses the System.Runtime.Caching
namespace. See: System.Runtime.Caching namespace.
Also, caching should be in a seperate layer between your controller (or business layer) and the data access layer, this is just for clarity.
这篇关于在 Post 模型中保留 SelectList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!