开发者

MVC, Forms and current route parameters / ID

开发者 https://www.devze.com 2023-02-03 19:30 出处:网络
I have a form that POSTs back data to the same webpage. The form must be given a HTML ID so that my JQuery validation will work properly.

I have a form that POSTs back data to the same webpage. The form must be given a HTML ID so that my JQuery validation will work properly.

Currently if you want to add such an ID you have to add a load of other stuff first, such as controller name and action name etc (which seems a bit rubbish to me)

开发者_如何学Go
<% using (Html.BeginForm("addcredits", "customer", FormMethod.Post, new { id = "addcreditform" }))

Now my current page is /customers/addcredits/1 so I need to somehow get the "1" parameter into my Form - how do I do this without some kind of hack? I understand it needs to be a parameter after "customer" but am unsure the best way to get this data.


Your Controller should pass the data to the View.

Since you only want one integer to be passed to the View then the View page should have this inherit in the Page definition:

Inherits="System.Web.Mvc.ViewPage<int>"

And to display the integer somewhere on the page, for example in your form, you would do the following:

<%= Model %>

Model is the object that was passed to the View and is of the type int.

On the Controller you have to do the following when you post:

public ActionResult addcredits(int? id)
{
    // Some post logic
    return View(id.Value);
}

Voila! the id parameter is the number. I pass it back to the view.

0

精彩评论

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