简体   繁体   中英

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.

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. 在此处输入图片说明

尝试这个

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

Just use

ModelState.Clear();

before return View(yourmodel);

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM