我花了 5 天时间解决问题,但找不到问题。我在我的项目中使用K-MVC 提交数据。这是一个简单的例子:1)控制器
public class TechnologyController : KnockoutController
{
public ActionResult AddTechnology()
{
var model=new TechnologyViewModel();
return View(model);
}
[HttpPost]
public ActionResult AddTechnology(TechnologyViewModel viewModel)
{
if(ModelState.IsValid)
{
_serviceClient.AddTechnology(viewModel);
viewModel.SavedSaccessfully = true;
}
return Json(viewModel);
}
}
2) 型号
public class TechnologyViewModel
{
public int TechnologyId { get; set; }
[Required]
public string Name { get; set; }
public bool IsActual { get; set; }
public bool SavedSaccessfully { get; set; }
}
3) 剃须刀
@using PerpetuumSoft.Knockout
@model Core.ViewDataModel.TechnologyViewModel
@{
var ko = Html.CreateKnockoutContext();
ViewBag.Title = "New technology";
}
@using (ko.If(m => m.SavedSaccessfully))
{
@ko.Html.Span("NEW TECHNOLOGY SAVED TO DATABASE.")
}
@using (ko.If(m => m.SavedSaccessfully == false))
{
using (ko.Html.Form("AddTechnology", "Technology", ko.Model))
{
<p>@Html.Label("Name: ") @ko.Html.TextBox(m => m.Name).ValueUpdate(KnockoutValueUpdateKind.AfterKeyDown) </p>
<p>@Html.Label("Active : ") @ko.Html.CheckBox(m => m.IsActual).ValueUpdate(KnockoutValueUpdateKind.Change) </p>
</p>
<p>
<input class="submitButton" type="submit" value="Create" />
</p>
}
}
@ko.Apply(Model)
它工作正常。然后在控制器中接收到的无效模型将不会提交。但是,如果模型发回,则什么也不会发生。不会出现验证消息。我试着像标准的 MVC 一样:
using (ko.Html.Form("AddTechnology", "Technology", ko.Model))
{
Html.EnableClientValidation();
<p>@Html.Label("Name: ") @ko.Html.TextBox(m => m.Name, new{name="Name"}).ValueUpdate(KnockoutValueUpdateKind.AfterKeyDown) @Html.ValidationMessageFor(m=>m.Name) </p>
<p>@Html.Label("Active : ") @ko.Html.CheckBox(m => m.IsActual).ValueUpdate(KnockoutValueUpdateKind.Change) </p>
<p>
<input class="submitButton" type="submit" value="Create" />
</p>
}
但它不起作用。请帮忙!求救!---......----......----...... :)