Ajax.BeginForm没有调用action方法

时间:2013-11-27 12:27:39

标签: asp.net asp.net-mvc asp.net-mvc-4 html-helper

我在ym asp.net mvc razor视图中有以下代码:_

    @using (Ajax.BeginForm("ManageCustomAsset", "Customer", new AjaxOptions

    {
        InsertionMode = InsertionMode.InsertBefore,
        UpdateTargetId = "customassettableBody",
        LoadingElementId = "progress",
        HttpMethod= "POST",
        OnSuccess="submitform"
    }))
    {
    @Html.ValidationSummary(true)
   @Html.AntiForgeryToken()
         <div>
     <span class="f"> @Html.DisplayNameFor(model=>model.CustomAsset.CustomerName) </span>       
    @Html.TextBoxFor(model => model.CustomAsset.CustomerName, new { disabled = "disabled" })
    @Html.ValidationMessageFor(model => model.CustomAsset.CustomerName)                                              

     @Html.HiddenFor(a => a.CustomAsset.CustomerName)

    </div>


    <div>
        <span class="f"> @Html.DisplayNameFor(model=>model.CustomAsset.CustomAssetType.Name) </span> 
    @Html.DropDownListFor(model=>model.CustomAsset.CustomTypeID, ((IEnumerable<TMS.Models.CustomAssetType>)ViewBag.CustomTypes).Select(option => new SelectListItem {
            Text = (option == null ? "None" : option.Name), 
            Value = option.ID.ToString(),
            Selected = (Model != null) && (Model.CustomAsset != null) && (option.ID == Model.CustomAsset.CustomTypeID)
        }), "Choose...") <i class=" icon icon-blue icon-star-on "></i>
        @Html.ValidationMessageFor(model=>model.CustomAsset.CustomTypeID)
    </div>
           <div>
     <span class="f"> @Html.DisplayNameFor(model=>model.CustomAsset.Description) </span>       
    @Html.TextBoxFor(model => model.CustomAsset.Description)
    @Html.ValidationMessageFor(model => model.CustomAsset.Description)                                              



    </div>                                      






           <input type="submit" value="Save" class="btn btn-primary"/>

}

但是当我点击表单时,它将无法达到以下操作方法,加载图像将显示的是什么,然后什么都不会发生,我甚至不会收到错误???: - < / p>

[HttpPost]
       [ValidateAntiForgeryToken]
       [CheckUserPermissions(Action = "Edit", Model = "CustomAsset")]
       public ActionResult ManageCustomAsset(CustomerCustomAssetJoin ccaj)
       {

           if (ModelState.IsValid)
           {
               try
               {
                   string ADusername = User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1);
                   repository.InsertOrUpdateCustomerCustomAsset(ccaj.CustomAsset, ADusername);
                   //  repository.Save();

                   return PartialView("_customerCustomAsset", ccaj);

               }
               catch (Exception ex)
               {

                   //ModelState.AddModelError(string.Empty, "Error occurred: " + ex.InnerException.Message);
                   return Json(new { IsSuccess = "False", description = ex.InnerException.InnerException.Message.ToString() }, JsonRequestBehavior.AllowGet);
               }

           }

           return View(ccaj);


       }

2 个答案:

答案 0 :(得分:1)

这可能与防伪令牌有关。您在操作中实施了ValidateAntiForgeryToken属性,但在视图中已对@Html.AntiForgeryToken()进行了注释。

答案 1 :(得分:1)

你添加这个js吗?

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

示例:

型号:

public class MyViewModel
{
    [Required]
    public string Foo { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return Content("Thanks", "text/html");
    }
}

查看:

@model AppName.Models.MyViewModel

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

<div id="result"></div>

@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "result" }))
{
    @Html.EditorFor(x => x.Foo)
    @Html.ValidationMessageFor(x => x.Foo)
    <input type="submit" value="OK" />
}