开发者

How to not lose the collection that populates a DropDownList in ASP MVC 3?

开发者 https://www.devze.com 2023-03-03 07:10 出处:网络
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 li

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 upda开发者_C百科te 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.


The POST Index method should get all values for SalesPeople from the FormCollection. You can always use the Request.FormCollection to query for all form data. Another choice is to you some kind of caching DB data :). Anyways please attach you POST Index method's code


You should not have SalesPeople collection in your OrderSearchViewModel cause it violates single responsibility principle. You should override EditorTemplate for SalesPersonId and create separate action, that will be responsible for populating dropdown.

Here is code from my application, you can use it as example. Note that my CategoryId property has Guid type - you should replace Guid? with int?.

Template overriding using mvcextensions:

    public class CreateAccidentCommandMetadata : ModelMetadataConfiguration<CreateAccidentCommand>
{
    public CreateAccidentCommandMetadata()
    {
        Configure(x => x.TrackingNumber)
            .Required();

        Configure(x => x.CategoryId)
            .Required()
            .DisplayName("Category")
            .Template(MVC.Accident.Views.EditorTemplates.Category);
    }
}

Template content:

@model Guid?
@{Html.RenderAction(MVC.Category.List(ViewData.ModelMetadata, Model));}

Category.List action:

    public virtual ActionResult List(ModelMetadata modelMetadata, Guid? selected)
    {
        ViewData.Model = queryService.GetList(new GetCategoryListQueryContext())
            .Select(x => new SelectListItem
                             {
                                 Text = x.Name,
                                 Value = x.Id.ToString(),
                                 Selected = selected.HasValue && x.Id == selected
                             })
            .ToList();

        //it is important to set up ModelMetadata after model
        ViewData.ModelMetadata = modelMetadata;

        return View();
    }

and pretty simple view:

@model IEnumerable<SelectListItem>
@Html.DropDownList(ViewData.ModelMetadata.PropertyName, Model)
0

精彩评论

暂无评论...
验证码 换一张
取 消