Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind html controls without using ASP.Net MVC html helpers?

Tags:

asp.net-mvc

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

like image 435
Mou Avatar asked Mar 09 '15 15:03

Mou


1 Answers

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:

TextBox:

@Html.TextBoxFor(m=> m.Name)
<input name="Name" type="Text" value="@Model.Name"/>

DropDownList:

@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.

@Html.BeginForm():

@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>

UPDATE:

@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">

PasswordFor:

@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

like image 59
Ehsan Sajjad Avatar answered Oct 30 '22 07:10

Ehsan Sajjad