views:

27

answers:

2

I have a view called Associations that has a drop down list and a button. When the user selects an option and presses submit, I want them to go to Association/{associationKey}. Association needs to work on get and post.

Currently, with the code below, when the form is posted, it DOES go to Association and displays the correct record, BUT it does not append the associationKey to the url.

So I am getting:

http://localhost/Association

instead of:

http://localhost/Association/202

If I manually navigate to http://localhost/Association/202 everything works perfectly, so get and post are both working fine....I just want the key in the url after a post!

Surely there must be something super simple I am doing wrong. Relevant code below.

Thanks!

ASSOCIATIONS view:

<% Html.BeginForm("Association", "Staff", FormMethod.Post); %>
<%:Html.DropDownList("associationKey", new SelectList(Model.Associations.ToList(), "AssociationKey", "LegalName"))%>
<input type="submit" value="Edit The Selected Record" />
<% Html.EndForm(); %>

STAFF controller:


        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        public ActionResult Association(int associationKey)
        {
            return View("Association", new AssociationViewModel(associationKey));
        }

GLOBAL.asax:


        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute("Default", "{action}", new { controller = "Staff", action = "Default" });
            routes.MapRoute("Associations", "Associations", new { controller = "Staff", action = "Associations" });
            routes.MapRoute("Association", "Association/{associationKey}", new { controller = "Staff", action = "Association" });
        }

ASSOCIATION view model:


    public class AssociationViewModel
    {

        public Repository db = new Repository();

        public Association Association {get; private set; }

        public List TelephoneTypes { get; private set; }

        public AssociationViewModel(int associationKey)
        {
            Association = db.AssociationById(associationKey);
            TelephoneTypes = db.ListTelephoneTypes().ToList();
        }

    }
+1  A: 

It's doing a post instead of a GET. This puts the value in the form parameters not in the url. You might want to intercept the form submit using javascript and turn it into a GET using location=..

<script type="text/javascript">
    $(function() { // requires jQuery
        $('form').submit( function() {
            // maybe do some validation to ensure a legal value chosen first?
            location.href = $(this).attr('action') + '/' + $(this).find('select').value();
            return false;  // cancel submit
        });
    });
</script>

<% Html.BeginForm("Association", "Staff", FormMethod.Post); %> 
<%:Html.DropDownList("associationKey", new SelectList(Model.Associations.ToList(), "AssociationKey", "LegalName"))%> 
<input type="submit" value="Edit The Selected Record" /> 
<% Html.EndForm(); %>
tvanfosson
+2  A: 

I think you should separate out your controller actions into a Get action and a POST action like so:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Association(int associationKey)
{
    return View("Association", new AssociationViewModel(associationKey));
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Association(AssociationViewModel model)
{
    return RedirectToAction("Association", new {associationKey= model.associationKey});
}

The MVC framework will automatically bind the selected value from your SelectList to the model (assuming you have a property in the model to hold the selected value). From there you just need to redirect to your GET method passing in the key.

Ben Elder
The Association view is being POSTed to from the Associations view, so there is no AssociationViewModel to pass to the Association view. The only thing I have is the associationKey.
Blackcoil
By splitting into two controller actions, and having your POST action receive an AssociationViewModel parameter, the framework will instantiate and bind the model for you and pass it to the action method. Try it and put a break point and see what you get. Just make sure you add an empty constructor to the AssociationViewModel class first.
Ben Elder
The solution was twofold. First I split into GET and POST actions like you suggested, and pass in the FormCollection to the POST, since the associationKey is in there. Also, my routes were in the wrong order. Default routes come last, specific routes come first.
Blackcoil
Cool. Glad I was able to point you in the right direction - at least somewhat :)
Ben Elder