【问题标题】:can i insert html code and Razor code in same cshtml page我可以在同一个 cshtml 页面中插入 html 代码和 Razor 代码吗
【发布时间】:2015-08-26 12:46:07
【问题描述】:

我可以在同一页面中插入带有此 Razor CSHTML 代码的 html 设计/代码吗?

@model project_final.Models.Bike
@{
  ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
  @Html.ValidationSummary(true)
  <fieldset>
    @<legend>Bike</legend>
    @<div class="editor-label">
      @Html.LabelFor(model => model.Id)
    </div>
    <div class="editor-field">
      @Html.EditorFor(model => model.Id)
      @Html.ValidationMessageFor(model => model.Id)
    </div>
    <div class="editor-label">
      @Html.LabelFor(model => model.Color)
    </div>
    <div class="editor-field">
      @Html.EditorFor(model => model.Color)
      @Html.ValidationMessageFor(model => model.Color)
    </div>
    <p>
      <input type="submit" value="Create" />
    </p>
  </fieldset>
}

<div>
  @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")

【问题讨论】:

  • 你是什么意思“html设计/代码”?你指的是什么代码。为什么@&lt;legend&gt;Bike&lt;/legend&gt; 和@&lt;div class="editor-label"&gt; 中有@ 符号?
  • 你试过了吗?答案是肯定的还是否定的?只需尝试并观察会发生什么,就可以非常简单地解决这个问题。
  • Razor 只是 html 的标记语法。您绝对可以嵌入 HTML。

标签: asp.net-mvc


【解决方案1】:

是的,你可以。

请注意Razor引擎中需要@的代码是什么,html标签不需要@Stephen Muecke提到的@ . @Bike 不需要前面的 @ 因为是 html 标签。或者我做一个简单的声明

@从不坚持@,它一定是 错误

阅读Introduction to ASP.NET Web Programming Using the Razor Syntax (C#),也许你可以了解更多关于 Razor 的知识。

顺便说一句,您可能需要阅读帖子How can I add a class attribute to an HTML element generated by MVC's HTML Helpers? 来修改HTML 助手的类,例如@Html.LabelFor

@Html.LabelFor(model => model.Id, new { @class = "col-md-2 control-label" }) 

【讨论】:

    猜你喜欢
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多