开发者

multiple forms in an ASP.NET MVC view, and routing issues

Say you have an MVC view for editing a Sandwich: sandwich name, price, etc. This form has its own Submit button. When you submit the form, the Edit POST action is called, the sandwich is updated, and the View is reloaded.

Then on the same view, below the Sandwich Edit form, we have a drop down list of ingredients with an Add button next to it. How do I make the Add Ingredient form post to a different Action, but then reload the Edit view?

RedirectToAction("Edit") puts a lot of junk up in the URL.

Here is one way I have tried that works, but puts junk in the URL:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult LoginRemoveAssociation(FormCollection values)
    {
        int webUserKey = Int32.Parse(values["WebUserKey"]);
        int associationKey = Int32.Parse(values["AssociationKey"]);
        db.DeleteWebUserAssociation(webUserKey, associationKey);
        return RedirectToAction("LoginEdit", new LoginEditViewModel(webUserKey, true));
    }

Here is the junk in the URL after the RedirectToAction:

https://loca开发者_StackOverflow社区lhost/mvc/Admin/Login/382?WebUser=Web.Data.Entities.WebUser&Associations=System.Data.Objects.ObjectQuery`1[Web.Data.Entities.Association]&WebUserAssociations=System.Data.Objects.DataClasses.EntityCollection`1[Web.Data.Entities.WebUserAssociation]&ManagementCompanies=System.Collections.Generic.List`1[Web.Data.Entities.ManagementCompany]&ManagementCompanyList=System.Web.Mvc.SelectList&AccessLevels=System.Collections.Generic.List`1[Web.Data.Entities.AccessLevel]&AccessLevelList=System.Web.Mvc.SelectList&PostMessage=Changes%20saved.


The reason why your getting "junk" in your url is because your passing a LoginEditViewModel to the edit action. .Net is using trying to convert the object to a name so it can pass it in a parameter. Which is why your seeing Web.Data.Entities.......

What does your Edit controller look like?

If its something like:

public ActionResult Edit(int id)

Then your redirect to action should be something like:

return RedirectToAction("Edit", new { id = 1 });

Rather than passing your view model.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜