我無法得到不顯眼的驗證工作使用自定義viewModels代表我的EF生成的類的抽象。viewmodels和不顯眼的驗證mvc3
盡我所能,驗證並未針對我城市實體中的屬性Name
的表單提交進行觸發。我相信這是與觀點中的不同模型有關,但我根本不知道它是如何工作的。
請注意。我擁有所有最新的驗證腳本,當使用firebug和firequery觀察頁面時,我可以看到腳本添加並從輸入中刪除有效的類,但輸入不是驗證集合的一部分。
非常感謝提前。
我的視圖模型:
/// <summary>
/// Represents abstraction of the City View that also serves in
/// data binding between the City View and the City Model.
/// </summary>
public class CityViewModel
{
/// <summary>
/// Gets or sets the city.
/// </summary>
/// <value></value>
public City City { get; set; }
/// <summary>
/// Gets or sets the collection of states.
/// </summary>
/// <value></value>
public ICollection<State> States { get; set; }
}
我CreateCity觀點:
@model OzFarmGuide.ViewModels.CityViewModel
@{
ViewBag.Title = "Create a new city";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<h2>
Create a new city</h2>
@using (Html.BeginForm())
{
@Html.EditorFor(model => model.City, new { States = Model.States })
<div class="entity-actions">
<input type="submit" value="Create" />
|
@Html.ActionLink("Back to List", "Cities")
</div>
}
我的編輯模板:(_ValidationPartial只包含腳本引用)
@model OzFarmGuide.Models.City
@Html.Partial("_ValidationPartial")
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
@Html.DropDownList("StateId",
new SelectList(ViewBag.States as System.Collections.IEnumerable,
"StateId", "Name",
Model.StateId))
</div>
@Html.HiddenFor(model => model.CityId)
如這裏要求是腳本我已包括:
<script src="@Url.Content("http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
您包含哪些Js腳本?我有一種感覺,jquery.unobtrusive-ajax.js不包括在內。 – 2011-04-25 18:50:37
@amit_g:我沒有包含該腳本,但據我瞭解,只有ajax帖子需要。我已經添加了參考,但遺憾的是沒有改變。 – 2011-04-26 00:35:14
你能準確發佈哪些腳本?這些可能會在_AdminLayout.cshtml中。 – 2011-04-26 01:19:07