简体   繁体   English

如何在MVC3中使用剃刀写入输入值

[英]How to write input value with razor in MVC3

I use resources file in MVC3 project for multilanguages. 我将MVC3项目中的资源文件用于多语言。

[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. @Html.LabelFor(o => o.Search)在页面上有效,但我需要输入输入值。

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

i tried this but value is empty in html page source. 我试过了,但html页面源中的值为空。

Using Razor syntax you can write your code like this: 使用Razor语法,您可以这样编写代码:

@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. 如果要在get方法期间在文本框中填充值,则只需将值分配给Search属性,然后将模型传递给视图即可。 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. 现在,您将可以看到在页面加载时,文本“ Hello World”已分配给您的文本框。 在此处输入图片说明

尝试这个

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

Just use 只需使用

ModelState.Clear();

before return View(yourmodel); return View(yourmodel);

You can try like this 你可以这样尝试

in .aspx view file 在.aspx视图文件中

<%: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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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