【问题标题】:CSS working fine for HTML but not Razor syntaxCSS 适用于 HTML 但不适用于 Razor 语法
【发布时间】:2019-02-19 18:19:01
【问题描述】:

我正在制作联系表格,起初它是纯 html:

<div id="contact-container">
<form class="cf">
    <div class="half left cf">
        <input type="text" id="input-name" placeholder="Name">
        <input type="email" id="input-email" placeholder="Email address">
        <input type="tel" id="input-phone" placeholder="Phone">
        <input type="text" id="input-subject" placeholder="Subject">
    </div>
    <div class="half right cf">
        <textarea name="message" type="text" id="input-message" placeholder="Message"></textarea>
    </div>
    <input type="submit" value="Submit" id="input-submit">
</form>
</div>

但是,当我将其转换为 Razor 语法时:

<div id="contact-container">
@Html.BeginForm("Contact", "Service", FormMethod.Post, new { @class = "cf" })
{
    <div class="half left cf">
        @Html.TextBoxFor(m => m.Name, new { htmlattributes = new { id = "input-name", placeholder = "Name" }})
        @Html.TextBoxFor(m => m.Email, new { htmlattributes = new { id = "input-email", placeholder = "Email Address" }})
        @Html.TextBoxFor(m => m.Phone, new { htmlattributes = new { id = "input-phone", placeholder = "Phone" }})
        @Html.TextBoxFor(m => m.Subject, new { htmlattributes = new { id = "input-subject", placeholder = "Subject" }})
    </div>
    <div class="half right cf">
        @Html.TextBoxFor(m => m.Message, new { htmlattributes = new { id = "input-message", placeholder = "Message" }})
    </div>
    <input type="submit" value="Submit" id="input-submit">
}
</div>

看起来完全不同,网页上有System.Web.Mvc.Html.MvcForm {之类的随机文本和一堆分号。我在这里错过了什么?

【问题讨论】:

    标签: html css razor


    【解决方案1】:

    需要在创建表单的周围加上using,例如:

    <div id="contact-container">
    @using (Html.BeginForm("Contact", "Service", FormMethod.Post, new { @class = "cf" })
    {
        <div class="half left cf">
            @Html.TextBoxFor(m => m.Name, new { htmlattributes = new { id = "input-name", placeholder = "Name" }})
            @Html.TextBoxFor(m => m.Email, new { htmlattributes = new { id = "input-email", placeholder = "Email Address" }})
            @Html.TextBoxFor(m => m.Phone, new { htmlattributes = new { id = "input-phone", placeholder = "Phone" }})
            @Html.TextBoxFor(m => m.Subject, new { htmlattributes = new { id = "input-subject", placeholder = "Subject" }})
        </div>
        <div class="half right cf">
            @Html.TextBoxFor(m => m.Message, new { htmlattributes = new { id = "input-message", placeholder = "Message" }})
        </div>
        <input type="submit" value="Submit" id="input-submit">
    }
    </div>
    

    【讨论】:

    猜你喜欢
    • 2015-01-02
    • 2021-07-30
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    相关资源
    最近更新 更多