erkan demir
erkan demir

Reputation: 1416

How to write input value with razor in MVC3

I use resources file in MVC3 project for multilanguages.

[Display(Name = "Search", ResourceType = typeof(LanguageResources.Lang))]
public string Search { get; set; }

I cant write an input value with razor.

@Html.LabelFor(o => o.Search) is works on page but i need to write value of inputs.

<input class="searchInput" type="text" name="name" value='@Html.ValueFor(o => o.Search)' />

i tried this but value is empty in html page source.

Upvotes: 4

Views: 17378

Answers (4)

Balaji Kondalrayal
Balaji Kondalrayal

Reputation: 1751

Just use

ModelState.Clear();

before return View(yourmodel);

Upvotes: 1

Jitender Kumar
Jitender Kumar

Reputation: 2587

Using Razor syntax you can write your code like this:

@Html.TextBoxFor(o => o.Search)

If you want to fill the value in your textbox during your get method, you just need to assign the value to your Search property and pass the model to your view and that is that. See the code below:

model.Search = "Hello World";
return View(model);

Now you will be able to see that text "Hello World" is assign to your textbox when the page get load. enter image description here

Upvotes: 3

Atish Kumar Dipongkor
Atish Kumar Dipongkor

Reputation: 10422

try this

<input class="searchInput" type="text" name="name" value='@Model.Search' />

Upvotes: 2

Keren Caelen
Keren Caelen

Reputation: 1466

You can try like this

in .aspx view file

<%:Html.TextboxFor(o=>o.Search)%>

in razor view:

@Html.TextBoxFor(o=>o.Search)

In razor it can also be like:

@Html.EditorFor(o => o.Search)

Upvotes: 0

Related Questions