坚持下拉信息时的ModelState是无效的 [英] Persisting Dropdown information when ModelState is not valid

查看:136
本文介绍了坚持下拉信息时的ModelState是无效的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的DropDownLists一些问题,因为当我发布的信息,我的模型是无效它回来空页面触发一个错误酷似<一个href=\"http://stackoverflow.com/questions/3393521/the-viewdata-item-that-has-the-key-my-key-is-of-type-system-string-but-must\">this问题。

I'm having some issues with my DropDownLists, because when I post the information and my Model is not valid it comes back "empty" to the page triggering an error exactly like this question.

我使用提出了有解决方案,它解决了我的问题。无论如何,我想避免每次我的ModelState是无效的查询数据库,我带着这种方法。我想知道这是否是有效的,或者有更好的方法,现在做到这一点,考虑到不是MVC2(这是从<一个MVC的版本href=\"http://stackoverflow.com/questions/3393521/the-viewdata-item-that-has-the-key-my-key-is-of-type-system-string-but-must\">question)现在,我使用MVC 5,也许他们添加新的东西来解决这一点。

I've used the solution proposed there and it fixed my problem. Anyway, I wanted to avoid querying the database every time my ModelState is not valid and I came with this approach. I would like to know if it is valid or if there are better ways to do it now, considering that instead of MVC2 (which was the MVC version from the question) I'm now using MVC 5, maybe they added something new to tackle this.

我所做的是使用TempData的坚持信息时,我的模型是无效的。

What I've done was to use the TempData to persist the information when my model is not valid.

public class ViewModel
{
    [DisplayName("Project")]
    public int ProjectID { get; set; }
    public List<SelectListItem> Projects { get; set; }

    //Other fields
}

现在我的Create()行为(即填充项目)

Now my Create() Action (that populates Projects)

[HttpGet]
public ActionResult Create()
{
    ViewModel vmodel = new ViewModel();
    vmodel.Projects = db.GetProjects(User.Identity.Name).Select(x => new SelectListItem { Text = x.Description, Value = x.Id }).ToList();
    TempData["Projects"] = vmodel.Projects;

    return View(vmodel);
}

和我的文章会是这样的:

And my post would be like this:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ViewModel vmodel)
{
    //Clear TempData (in theory will clear my tempdata when read, so if this controller redirects to another action my tempdata will be clear)
    List<SelectListItem> projects = (TempData["Projects"] as List<SelectListItem>);

    if (ModelState.IsValid)
    {
        //...
    }

    //If it got here it's going back to the screen.
    //Repopulate the TempData (allowing it to exist one more trip)
    TempData["Projects"] = projects;
    vmodel.Projects = projects

    return View(atendimento);
}

这是一个好的方法吗?有没有更好的方式来实现,如果没有查询数据库每一次?

Is this approach a good one? Is there a better way to achieve that without querying the database every single time?

非常感谢!

推荐答案

您不需要像你在你的视图模型属性使用的TempData 在所有持有下拉项目。

You don't need to use TempData at all as you have a property in your view model to hold the dropdown items.

public ActionResult Create()
{
    ViewModel vmodel = new ViewModel();
    vmodel.Projects = GetProjects(); 
    return View(vmodel);
}
private List<SelectListItem> GetProjects()
{
     return db.GetProjects(User.Identity.Name)
                 .Select(x => new SelectListItem { Text = x.Description,
                                                   Value = x.Id }).ToList();   
}

和视图

@Html.DropDownListFor(s=>s.ProjectID,Model.Projects)

而在你的HttpPost行动,如果ModelState中是无效的,刷新了再度项目集合(因为HTTP是无状态

if(ModelState.IsValid)
{
  // to do :Save and redirect
}
model.Projects = GetProjects(); 
return View(model);

您可以缓存项目,这样你就不需要每次都打的数据库,如果你对性能过于担心。

You may cache the Projects so that you do not need to hit the database every time, if you are too much worried about performance.

这篇关于坚持下拉信息时的ModelState是无效的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆