views:

353

answers:

1

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

A: 

Ok, now I understand what you want.

Well, the thing is that the lisbox , on form submit is not sent to the server - only it's selected values are sent. So on the server you won't find the item list.

You could try to select all items in the listbox when the user presses submit.

Or you could : keep a hidden input field with the list of ids, separated by comma . Then when you remove from the listbox, you remove from the field. And on the server you will have a string parameter for the action and just parse that.

PS:

I tried the first solution with :

onclick='$("#remainingIds option").attr("selected","selected");'

And on the server "IList<int> remainingIds" had all the items. It should do the trick.

sirrocco
Thanks for the answer, 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
David E
thanks for replying again. Works great, hadn't thought of selecting all items (had gone for hidden text box)Many thanksDave
David E
With pleasure mate.
sirrocco