couldn't get model in action on post?

2020-05-09 23:16发布

问题:

I have a strongly typed view which has text fields and a submitted link. After editing data I press link and try to submit form. Placing a break point I am able to see control comes to action; modle is not null but all of its properties are always null, not sure where I m doing wrong. My tiny view code is:

@model BL.Model.Speaker
@using (Html.BeginForm())
{
    <table>
        <tr>
            <td>@Html.EditorFor(s => @Model.Name)</td>
            <td>@Html.EditorFor(s => @Model.Email)</td>
        </tr>
    </table>    
    @Html.ActionLink("Submit", "All");
}

and my controller action is:

 public ActionResult All(Speaker model){   
            return View(database.Speakers.FirstOrDefault());
        }

Help please

回答1:

You should change your button from ActionLink to a submit button like this

@Html.ActionLink("Submit", "All");

By

<input type="submit" value="submit"/>

Also change your Action to recieve the data by post

[HttpPost]
public ActionResult All(Speaker model)
{   
    return View(database.Speakers.FirstOrDefault());
}