Hi,
I have a page which has an Html.DropDownList which takes in a viewdata parameter. The issue is that when a user clicks save it verifies the model with a call to ModelState.IsValid if this fails I want the page to display the errors (this works fine)
The issue I have is that the viewdata for the DropDown is lost, this means that I don't know what state the listbox was in before the errors were validated (there is an ajax calls which allows things to be added and subtracted from the list - agin this works fine in isolation)
Edit -- As discussed below in reply to an answer: In effect I need the entire listbox back. The currently selected value isn't needed. Basically the list contains authors who aren't tagged to an article. When a user adds an author a Javascript function removes it from the list and adds it to a textbox which I can track. My issue is that I can't seem to get the current state of the listbox back (I appreciate the the viewstate is in effect lost once the request finishes) -- End edit (hope this makes it clearer)
The code is as follows:
On edit page
${Html.DropDownList("AuthorsDropDown")} <input type="button" onClick="removeOptions(Authors)"; value='Add author to article' />
On edit action of controller
public ActionResult Edit(Article form)
{
var articleList = articleMapper.MapForLists(form);
var authorList = new AuthorRepository().FindAll();
foreach (var author in articleList.Authors)
authorList.Remove(author);
ViewData["AuthorsDropDown"] = new SelectList(authorList, "Id", "GetFullName");
return View("Edit", articleList);
}
on save action
[ValidateModel(typeof (ArticleDto)), AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
public ActionResult Save(ArticleDto form)
{
return SaveContent(form, articleDto => RedirectToAction<NewsController>(c => c.Index(null)),PopulateIdentities);
}
It has been a long week so I might just be being a bit slow.
Thanks for taking the time to look