i am new in MVC. i have seen in mvc people generate html this way and bind data also
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(m => p.Name, "Name:")
@Html.TextBoxFor(m => p.Name)
</div>
<input type="submit" value="Create" />
}
but if i do not want to use html helper extension then how should i code to bind normal html controls and forms with model data. please come with a sample code. thanks
You have to use name
attribute of input
controls to match your model property name.
For Example in your current case if you want to replace TextBoxFor()
with input
control, do like this:
@Html.TextBoxFor(m=> m.Name)
<input name="Name" type="Text" value="@Model.Name"/>
@Html.DropDownListFor(m=>m.SomeProperty,ItemsList)
<select name="SomePropertyType">
<option value="someValue">Some Text</option>
</select>
The same way we have to do for DropDown List case.
@using(Html.BeginFor("ActionName","ControllerName",FormMethod.Post))
{
}
in HTML For form, you have to use form tag:
<form action="@Url.Action("ActionName","ControllerName")" method="post">
</form>
@Html.TextBoxFor(m=>m.SomeProperty")
in html as:
<input id="SomeProperty" name="SomeProperty" type="text" value="@Model.SomeProperty" />
@Html.TextAreaFor("SomeName", "", 10, 50, null)
in html as :
<textarea cols="50" id="SomeName" name="SomeName" rows="10">
@Html.PasswordFor(m=>m.SomeProperty)
in html as:
<input id="SomeProperty" name="SomeProperty" type="password" />
See this article of Scottgu for reference and also this CodeProject article
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With