如何不失去填充在ASP MVC 3 DropDownList中集? [英] How to not lose the collection that populates a DropDownList in ASP MVC 3?

查看:103
本文介绍了如何不失去填充在ASP MVC 3 DropDownList中集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的ASP MVC 3,我有以下问题。我有一个表格,使一个POST请求本身将数据发送到服务器。该表格​​包含了销售人员的一个DropDownList而这个名单是从数据库填充。其主要作用是这样的:

 公众的ActionResult指数()
{
    VAR视图模型=新OrderSearchViewModel();
    viewModel.SalesPeople = GetSalesPeopleList(); //这个查询数据库
    返回视图(视图模型);
}

和视图模型是这样的:

 公共类OrderSearchViewModel
{
        公共字符串ID {搞定;组; }
        公众的DateTime?起始日期{搞定;组; }
        公众的DateTime?结束日期{搞定;组; }
        公共字符串邮报code {搞定;组; }
        公众诠释SalesPersonId {搞定;组; }
        公共字符串SalesPersonRef {搞定;组; }
        公众诠释订单类型{搞定;组; }
        公共字符串系列号{搞定;组; }
        公共字符串CustomerPO {搞定;组; }
        公开名单<&业务员GT;销售人员{搞定;组; }
}

因此​​索引视图时,我提交的销售人员列表设置为空的形式(在指数HttpPost方法),我想以显示与填充仍列表中同样的观点。然而,要做到这一点,我将不得不再次查询数据库。什么是避免这样做的最佳做法?

编辑:

我的帖子指数moethod code是这样的:

  [HttpPost]
公众的ActionResult指数(OrderSearchViewModel视图模型)
{
   VAR的结果= QueryOrders(视图模型);
   // code更新的结果模型
   返回视图(视图模型);
}


解决方案

使用Ajax提交表单,只是使用JSON返回岗位操作的结果到客户端 - 这样的数据库没有该列表打两次,用户也得到流畅的体验。

I'm new to ASP MVC 3 and I have the following question. I have a form that makes a post request to itself to send data to the server. The form contains a DropDownList for the Sales People and this list is populated from the database. The main action looks like this:

public ActionResult Index()
{
    var viewModel = new OrderSearchViewModel();
    viewModel.SalesPeople = GetSalesPeopleList(); // This queries the DB
    return View(viewModel);
}

And the ViewModel looks like this:

public class OrderSearchViewModel
{
        public string Id { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
        public string PostCode { get; set; }
        public int SalesPersonId { get; set; }
        public string SalesPersonRef { get; set; }
        public int OrderType { get; set; }
        public string SerialNo { get; set; }
        public string CustomerPO { get; set; }
        public List<SalesPerson> SalesPeople { get; set; }
}

So in the Index view when I submit the form the SalesPeople list is set to null (in the Index HttpPost method) and I want to show the same view with the list still populated. Nevertheless, to do this I would have to query the database again. What's the best practice to avoid doing this?

EDIT:

My POST Index moethod code is something like this:

[HttpPost]
public ActionResult Index(OrderSearchViewModel viewModel)
{
   var result = QueryOrders(viewModel);
   //code update the model with the results
   return View(viewModel);
}

解决方案

Submit the form using Ajax and just return the outcome of the post operation to the client using json - that way the database isn't hit twice for that list and the user also gets a smoother experience.

这篇关于如何不失去填充在ASP MVC 3 DropDownList中集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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