简体   繁体   English

剃刀未正确渲染视图模型元素

[英]Razor not rendering view model element correctly

I have an input element and the value is coming from my view model 我有一个输入元素,值来自我的视图模型

<input id="modifyRuleCity" name="City" type="text" class="form-control" placeholder="City" value=@Model.City>

the value in the viewmodel is "San Francisco", but for some reason the Francisco part is being chopped off. 视图模型中的值是“ San Francisco”,但由于某种原因,该Francisco部分被砍掉了。 Here is what the rendered HTML looks like. 这是呈现的HTML外观。

<input id="modifyRuleCity" name="City" type="text" class="form-control" placeholder="City" value="San" francisco>

I tried 我试过了

value="@Model.City"

and

value=@Model.City

but had no luck it still chops off Francisco. 但没有运气它仍然砍掉弗朗西斯科。 I looked at the value and I don't see any " in the string in the viewmodel! 我查看了值,但在viewmodel的字符串中看不到任何“”!

Quotation marks. 引号。

<input id="modifyRuleCity" name="City" type="text" class="form-control" placeholder="City" value="@Model.City">

or 要么

<input id="modifyRuleCity" name="City" type="text" class="form-control" placeholder="City" value='@Model.City'>

As far I Understand:- 据我了解:

  1. value = "@Model.City" should have worked for you unless you don't make mistake another place. value = "@Model.City"应该为您服务,除非您别误会其他地方。
  2. If City contains " or ' you can try value = "@Html.Raw(Model.City)" 如果“城市”包含“或”,则可以尝试value = "@Html.Raw(Model.City)"
  3. If It still it is not working for you, you can try @Html.EditorFor(m => m.City, new { htmlAttributes = new { @id = "modifyRuleCity", @class = "form-control", Name = "City", PlaceHolder = "City" } }) 如果仍然无法使用,可以尝试@Html.EditorFor(m => m.City, new { htmlAttributes = new { @id = "modifyRuleCity", @class = "form-control", Name = "City", PlaceHolder = "City" } })

Hope It helps to you. 希望对您有帮助。

if you are using Razor then I would suggest to use HTML helpers such as the EditorFor and combining that with lambda expressions like so: 如果您使用的是Razor,则建议您使用HTML辅助程序(例如EditorFor) ,并将其与lambda表达式结合使用,例如:

@Html.EditorFor(model => model.City, new { htmlAttributes = new { @id = "modifyRuleCity", @class = "form-control", @name = "City", @placeholder = "City"} })

that will render the same thing as <input type="text" /> <input type="text" />一样的东西

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

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