2

In my mvc4 application i want to use html.beginform() to pass multiple parameters to an actionResult on the same controller.

I do it like this:

view:

<div>
    @using (Html.BeginForm("AddNote", "Lead",FormMethod.Post, null))
    {
        @Html.Hidden("leadID",@Model.ID)
        <input type="text" name="noteBody" />
        <input type="submit" class="mainButton" value="Add New!"/>
    }
</div>

Controller (LeadController):

[HttpPost]
ActionResult AddNote(int leadID, string noteBody)
{
    Note note = new Note();
    note.DateModified = DateTime.Now;
    note.Title = "No Title";
    note.Body = noteBody;

    Lead lead = unitOfWork.LeadRepository.GetById(leadID);
    lead.Notes.Add(note);

    unitOfWork.Save();

    return RedirectToAction("Details", new { id = leadID });
}

when i press the submit button i get an exception:

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Lead/AddNote

this is the place to say that i have tried it withput parameters and it worked just fine. I've also tried to pass the "leadID" parameter inside the form declaration (new {leadID = @Model.ID}).

Any idea what am i doing wrong ?

2
  • Do you have the [HttpPost] attribute on your AddNote action? Are you using some custom routes? Commented Dec 21, 2013 at 12:49
  • I do have the [HttpPost] on my action. seems like this is not the root cause. what do you mean by custom routs? Commented Dec 21, 2013 at 14:22

3 Answers 3

1

Just add the 'public' modifier to your action and it will do the magic.

Sign up to request clarification or add additional context in comments.

Comments

1

The AddNote method should be public. Use the public keyword and it will work.

2 Comments

Please expand on what you mean here; why does making it public work?
Controller is a class like other classes... you can't access private methods, properties, fields,... from outside. Just make it public, and it should work (if you didn't mess with routing, like mentioned). BTW, if you hover your mouse pointer over the AddNote string in the view (where the @using (Html.BeginForm("AddNote",... is), does it say: "Cannot resolve action 'AddNote'?
0

Add the HTTPPOST attribute , like this

[HttpPost]
ActionResult AddNote(int leadID, string noteBody)
{
    Note note = new Note();
    note.DateModified = DateTime.Now;
    note.Title = "No Title";
    note.Body = noteBody;

    Lead lead = unitOfWork.LeadRepository.GetById(leadID);
    lead.Notes.Add(note);

    unitOfWork.Save();

    return RedirectToAction("Details", new { id = leadID });
}

Perhaps it helps you

Comments

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.