質問

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 ?

役に立ちましたか?

解決

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

他のヒント

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

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top