I am having issues with passing an argument from my view to my controller
I have this code fragment in my view:
<a class="btn-simple" onclick="submitDetails('Dashboard', 'Cancel', '<%=Model.Data.Id %>', $('form'));">save</a>
and this is from my controller:
[HttpPost]
public ActionResult Cancel(string id, FormCollection collection)
{
var appt = Application.Session.GetObjectFromOid<Appointment>(new ObjectId(id));
}
I get an error saying that "id" is null, but when I test Model.Data.Id in the view, it is not null.
And this is my submitDetails function:
function submitDetails(controller, method, id, form) {
form.ajaxSubmit({ target: '#itemdetails' });
}
What I am trying to accomplish is getting back to my controller from a button in the view, the "[HttpPost]" version of the method that originally called the view
Make sure that the form you are submitting contains a hidden field with the id or it is passed as query string parameter:
<% using (Html.BeginForm("Dashboard", "Cancel", new { id = Model.Data.Id })) { %>
...
<% } %>
or if you want to pass a dynamic url you could do the following:
<a class="btn-simple" onclick="submitDetails('<%= Url.Action("Dashboard", "Cancel", new { id = Model.Data.Id }) %>', $('form'));">save</a>
and then:
function submitDetails(url, form) {
form.ajaxSubmit({ url: url, target: '#itemdetails' });
}
UPDATE:
And if you want to avoid the submitDetails
javascript method and have your anchor be the submit button of the form:
<% using (Html.BeginForm("Dashboard", "Cancel", new { id = Model.Data.Id })) { %>
...
<button class="btn-simple" type="submit">save</button>
<% } %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.