.net 如何在 Razor 语法中设置 @Html.TextBoxFor 中的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13989980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:48:06  来源:igfitidea点击:

How to set value in @Html.TextBoxFor in Razor syntax?

.netasp.net-mvcasp.net-mvc-3razorhtml.textboxfor

提问by viki

I have created an text-box using Razor and trying to set valueas follows.

我使用 Razor 创建了一个文本框,并尝试进行value如下设置。

@Html.TextBoxFor(model => model.Destination, new { id = "txtPlace", value= "3" })

I have tried appending valuewith @

我曾尝试追加value@

@Html.TextBoxFor(model=> model.Destination, new { id = "txtPlace", @value= "3" })

even though it renders html inputtag with empty value

即使它呈现input为空的html标签value

<input id="txtPlace" name="Destination" type="text" value 
   class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset ui-mini" >

What am doing wrong?

做错了什么?

回答by Gaz Winter

The problem is that you are using a lower case v.

问题是您使用的是小写的 v。

You need to set it to Value and it should fix your issue:

您需要将其设置为 Value ,它应该可以解决您的问题:

@Html.TextBoxFor(model => model.Destination, new { id = "txtPlace", Value= "3" })

回答by viki

I tried replacing valuewith Valueand it worked out. It has set the valuein inputtag now.

我尝试替换valueValue并成功了。它现在已经设置了valueininput标签。

@Html.TextBoxFor(model => model.Destination, new { id = "txtPlace", Value= "3" })

回答by dove

It is going to write the value of your property model.Destination

它将写下您财产的价值 model.Destination

This is by design. You'll want to populate your Destination property with the value you want in your controller before returning your view.

这是设计使然。在返回视图之前,您需要使用控制器中所需的值填充 Destination 属性。

回答by Pankaj

I tried replacing valuewith Valueand it worked out. It has set the valuein inputtag now.

我尝试替换valueValue并成功了。它现在已经设置了valueininput标签。

回答by Bogdan Mates

This works for me, in MVC5:

这对我有用,在 MVC5 中:

@Html.TextBoxFor(m => m.Name, new { @class = "form-control", id = "theID" , @Value="test" })

回答by Durgesh Singh

Tries with following it will definitely work:

@Html.TextBoxFor(model => model.Destination, new { id = "txtPlace", Value= "3" })

@Html.TextBoxFor(model => model.Destination, new { id = "txtPlace", @Value= "3" })

<input id="txtPlace" name="Destination" type="text" value="3" class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset ui-mini" >