Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
179 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...