1

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

7
  • Please post the submitDetails() code. Commented Mar 7, 2012 at 15:22
  • What does submitDetails do? Commented Mar 7, 2012 at 15:22
  • because your just passing id back as a string. The string doesn't know the same things your model knows. In this case it is looking for id as part of the url string i.e. controller/view/id. you have to pass the id from the model. Commented Mar 7, 2012 at 15:25
  • I am very new at this so I might not be doing what I am trying to do at all. I just want to get to the [HttpPost] version of Cancel() when I press the "Save" Button Commented Mar 7, 2012 at 15:26
  • <%=Model.Data.Id %> - Are you sure Model.Data isn't null? Commented Mar 7, 2012 at 15:29

1 Answer 1

1

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>
<% } %>
Sign up to request clarification or add additional context in comments.

12 Comments

I would like to avoid using submitDetails altogether if that is possible
@user1229309, in this case you could make this anchor be the submit button for your form. I have updated my answer with an example.
then you can add a hidden field as DD suggested with the value of model.data.id and grab that in the form collection. <input type="hidden" id="id" value="<%Model.Data.Id%>" />
@user1229309, how does the action attribute of the form look like? Does it contain the id? Like for example action="/Dashboard/Cancel/123"?
@DarinDimitrov I dont know what you mean by the action attribute
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.