Reputation: 4266
I'm making a little form that allow the user to confirm that he reallly want to delete the item, but I can't get the id on the controller...
@using (Html.BeginForm("Delete", "Home", FormMethod.Post))
{
@Resources.Audit.Delete_Ask_Confirmation_Text
@Html.HiddenFor(model => model.audit.id)
<button type="submit" class="btn btn-warning">
<i class="glyphicon glyphicon-trash spaceAfterIcon"></i> @Resources.Audit.Delete_Ask_Confirmation_Button
</button>
}
=> In audit :
public int Id { get; set; }
Html
<div>
<form action="/Audit/Home/Delete" method="post" novalidate="novalidate">Lore Ipsum
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="audit_Id" name="audit.Id" type="hidden" value="2">
<button type="submit" class="btn btn-warning">
<i class="glyphicon glyphicon-trash spaceAfterIcon"></i> Please confirm the delete
</button>
</form>
</div>
Controller:
[HttpPost]
public ActionResult Delete(int id)
{// always null}
The id parameter is always null.
I've tried changing the method signature to:
Delete(int audit_Id),
Delete(int id),
Delet(string id),
Delete(string audit_id),
...
Thanks for your help!
Upvotes: 0
Views: 316
Reputation: 15866
MVC model binding handles parameters by name conversation. Your hidden input name is
name="audit.Id"
so your action should be
public ActionResult Delete(audit model){
var id = model.id;
}
OR
@DoctorMick's answer.
Upvotes: 0
Reputation: 6793
If you're trying to use form fields and the Html helper functions you'd need to have your model as the parameter of the Delete controller method. Having just the ID is more correct and you can make sure it is passed through by changing your form declaration and adding an object representing additional route values:
@using (Html.BeginForm("Delete", "Home", new { id = Model.audit.id }, FormMethod.Post))}
You'll also be able to delete the hidden input type.
Upvotes: 2