I have a partial view that should run a HttpPost on a controller, but its not. On my main page I have the following partial view
<div class="box">
@Html.Action("AddNote", "Member", new { MemberId = Model.Id })
</div>
The partial view is as follows -
@model GMS.Core.Models.AddMemberNoteModel
@using (Html.BeginForm("AddNote", "Member", FormMethod.Post))
{
@Html.HiddenFor(model => model.MemberId)
<div class="input-group">
@Html.EditorFor(model => model.Note, new { @class = "form-control", @placeholder = "Type note ..." })
@Html.ValidationMessageFor(model => model.Note)
<span class="input-group-btn">
<input type="submit" class="btn btn-primary btn-flat" value="Save" />
</span>
</div>
}
The trouble is when the submit button is pressed, the HttpPost method is not hit. When the page loads, the first actionresult is hit.
[Authorize]
public ActionResult AddNote(string memberId)
{
AddMemberNoteModel model = new AddMemberNoteModel();
model.MemberId = Convert.ToInt32(memberId);
return PartialView("AddNote", model);
}
[HttpPost]
public ActionResult AddNote(AddMemberNoteModel model)
{
model.DateCreated = DateTime.Now;
model.CreatedBy = ((CustomPrincipal)HttpContext.User).Id;
model.LastUpdated = DateTime.Now;
model.LastUpdateBy = ((CustomPrincipal)HttpContext.User).Id;
if (ModelState.IsValid)
{
_memberService.AddNote(model);
this.TempData["Notification"] = "A note has been added.";
return RedirectToAction("Details", "Member", new { id = model.MemberId });
}
else
{
return PartialView(model);
}
}
Rendered HTML
<form action="/Member/AddNote/7537" method="post"><input data-val="true" data-val-number="The field MemberId must be a number." data-val-required="The MemberId field is required." id="MemberId" name="MemberId" type="hidden" value="7537" /><div class="input-group">
<input class="text-box single-line" data-val="true" data-val-required="The Note field is required." id="Note" name="Note" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Note" data-valmsg-replace="true"></span>
<span class="input-group-btn">
<input type="submit" class="btn btn-primary btn-flat" value="Save" />
</span>
</div>
</form>
CodePudding user response:
The answer to this was I had the Add Note form embedded inside another form.
@(Html.BeginForm())
{
..........
@using (Html.BeginForm("AddNote", "Member", FormMethod.Post))
{
@Html.HiddenFor(model => model.MemberId)
<div class="input-group">
@Html.EditorFor(model => model.Note, new { @class = "form-control", @placeholder = "Type note ..." })
@Html.ValidationMessageFor(model => model.Note)
<span class="input-group-btn">
<input type="submit" class="btn btn-primary btn-flat" value="Save" />
</span>
</div>
}
}