【问题标题】:How can call model in layout in mvc 4 razor如何在 mvc 4 剃须刀的布局中调用模型
【发布时间】:2015-12-04 11:48:57
【问题描述】:

我想在布局中调用模型以在空文本字段中使用验证,以便在任何字段为空时向阵风用户返回错误。我有一个代码布局

@using (Html.BeginForm("Sending", "Home"))
{
 <form method="post" id="contactfrm" role="form">
   <div class="col-sm-4">
     <div class="form-group">
       <label for="name">Name</label>
         @Html.TextBoxFor("Name", null, new { id = "name", @class = "form-control", placeholder = "Enter name", title = "Please enter your name (at least 2 characters)" })
@Html.ValidationMessageFor(model => model.Name)
       </div>
       <div class="form-group">
       <label for="email">Email</label>
       @Html.TextBoxFor("Email", null, new { id = "email", @class = "form-control", placeholder = "Enter email", title = "Please enter a valid email address)" })
       @Html.ValidationMessageFor(model => model.Email)
       </div>
    </div>
    <div class="col-sm-4">
<div class="form-group">
<label for="comments">Comments</label>
 @Html.TextArea("Comment", null, new { id = "comments", @class = "form-control", TextMode = "MultiLine", Columns = "3", Rows = "5", placeholder = "Write you message here...", title = "Please enter your message (at least 10 characters)", Style = "resize:none" })
@Html.ValidationMessageFor(model => model.Comment)
</div>
  <button name="submit" type="submit" class="btn btn-lg btn-primary" id="submit">Send Your Message</button>
     <div class="result"></div>
     </div>
     </form>
}

我想调用sending.cs模型在@Html.ValidationMessageFor(model => model.Name)中使用

如何在布局中调用模型?

谢谢

【问题讨论】:

  • 那么当你将@Html.ValidationMessageFor(model => model.Name) 添加到你的代码中会发生什么?
  • 其他方法是添加“必需”HTML 属性

标签: c# razor


【解决方案1】:

像这样将你的模型添加到你的视图页面

@model nameOfModel

并像这样使用它

 @Html.ValidationMessageFor(model => model.Name) 

如果您只想使用其中的一部分,请这样做

@Model.nameOfproperty

【讨论】:

  • 我知道它可以与其他视图一起使用,但现在我认为我必须基于 jQuery 进行验证,它可以知道我在移动 take 或 muse 输入事件后验证
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-22
  • 2020-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多