坚持的SelectList在Post模型 [英] Persist SelectList in model on Post
问题描述
在MVC4:
我在我的模型中的下列财产用于下拉列表:
公共科目的SelectList {搞定;组; }
我在指数()在页面加载动作设置学科属性和返回模型。
在下拉列表被填充蛮好用SelectListItems。
@ Html.DropDownListFor(X => x.Subject,新的SelectList(Model.Subjects,文本,文本,其他))
当我提交表单模型中的主题的SelectList已更改为空。必须有坚持本上HttpPost的简单方法。我想我可以提交和发布此的SelectList,以及与所有的表单字段一起?我会怎么做呢?
人们普遍您重新填充的SelectList
在发布
的行动。只需提取它的方法里面并调用它在获取
和发表
操作。
张贴再次回到该控制器是不是要走的路。您可以缓存在选择列表中的项目,所以你就不必进行查询到数据存储的两倍。
例如:
公众的ActionResult的Create()
{
VAR模型=新SubjectModel();
PopulateSubjectList(模型);
返回查看(模型);
}[HttpPost]
公众的ActionResult创建(SubjectModel模型)
{
如果(ModelState.IsValid)
{
//保存项目..
}
// 出了些问题。
PopulateSubjectList(模型);
返回查看(模型);
}私人无效PopulateSubjectList(SubjectModel模型)
{
如果(MemoryCache.Default.Contains(SubjectList))
{
//该SubjectList已经存在于高速缓存中,
model.Subjects =(列表<个体GT;)MemoryCache.Default.Get(SubjectList);
}
其他
{
//选择列表尚不存在于缓存中,获取从数据存储项目。
清单<个体GT;选择列表= _db.Subjects.ToList(); //缓存存储器中的列表中15分钟。
MemoryCache.Default.Add(SubjectList,选择列表,DateTime.Now.AddMinutes(15));
model.Subjects =选择列表;
}
}
请注意:的MemoryCache
使用 System.Runtime.Caching
命名空间。请参阅: System.Runtime.Caching命名空间 。 p>
另外,高速缓存应该是在控制器(或业务层)和数据访问层之间的单独的层,这仅仅是为了清楚
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.
这篇关于坚持的SelectList在Post模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!