MVC TextBox with name specified not binding model on post

限于喜欢 提交于 2019-12-24 02:23:50

问题


This is like MVC 101 here so I feel completely helpless on why this isn't working. I have an extremely basic Model:

public class StockEnrollmentModel
{
    [Required]
    [DisplayName("Employee Name:")]
    public string EmployeeName { get; set; }

}

My view looks like this:

@using (Html.BeginForm("SimulateForm", "HR", FormMethod.Post))
{
<div class="row">
    <div class="col-md-6">
        <div class="form-group">
            @Html.LabelFor(e => e.EmployeeName)
            @Html.TextBox("stock_employee_name", Model.EmployeeName)
        </div>
    </div>
</div>
<button type="submit" class="btn btn-primary" value="Submit">Submit</button>
}

The web service I will be posting to requires specific names for the input fields in order to successfully receive the data

As in, the rendered html needs to read:

<input type="stock_employee_name" type="text" /> 

After much googling, I determined I need to use an Html.Text box in order to have control of the name attribute that is generated.

The problem I'm having is that when I submit the form, the model in my controller is completely void of data. Investigating this shows that the form posted to the server with "employee_stock_name=Some Name" rather than "EmployeeName=Some Name"

From my research, this shouldnt be happening, correct?? This exact situation should be the reason you use TextBox instead of TextBoxFor.

What am I missing?

Here is my controller for what its worth:

[HttpPost]
    public RedirectToRouteResult SimulateForm(StockEnrollmentModel model )
    {
        if ( ModelState.IsValid )
        {
            return RedirectToAction("SignForm", "HR", model);
        }
        return RedirectToAction("StockPurchase", model );
    }

UPDATE

The accepted answer below was what I eventually ended up using. There's no real way to easily change the name of the HTML field and maintain the MVC model binding. I ended up changing my property names to match what I needed the name field to read.


回答1:


The first parameter of the HtmlHelper.TextBox function is the name attribute of the input element created by the function. You're specifying "employee_stock_name" as that parameter (and thus as the name attribute of the input element), so that is what is being sent across the wire.

You should either specify the correct name:

@Html.TextBox("EmployeeName", Model.EmployeeName)

or use HtmlHelper.TextBoxFor to generate it automatically:

@Html.TextBoxFor(m => m.EmployeeName)



回答2:


If you really need your html to be rendered as <input name="stock_employee_name" type="text" />, you can add the following attribute to the property on your model: [Bind(Prefix = "stock_employee_name")] - but this is a hack and it sounds like you are misunderstanding a requirement somewhere else? Why does the input name need to be "employee_stock_name"?




回答3:


You can change the name of a rendered textbox from @Html.TextBoxFor by passing in the Name parameter to the htmlAttributes object parameter of the helper method.

@Html.TextBoxFor(model => model.EmployeeName, new { Name = "stock_employee_name" })

This will break your model binding when posting back to your controller but at least your name will be right on the form and the data will get filled when creating the form initially.

You can however, use Request.Form["stock_employee_name"] to retrieve the data in your controller's post Action.



来源:https://stackoverflow.com/questions/30359355/mvc-textbox-with-name-specified-not-binding-model-on-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!