K-MVC客户端和服务器端验证

时间:2012-08-30 08:30:57

标签: c# asp.net-mvc-3 knockout-mvc

我在问题上花了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)Razor

    @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>
 }

但它不起作用。请帮忙! SOS! ---... --- ...... --- ...... :)

0 个答案:

没有答案