Ajax.BeginForm перенаправляет страницу действий (MVC 4) - PullRequest
1 голос
/ 07 февраля 2012

Я использую Ajax.BeginForm в своем приложении, но по какой-то причине моя форма перенаправляет на {controller} / {action} с правильными данными json вместо start {OnComplete = "Add_OnComplete"}:

Вид:

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

    @using (Ajax.BeginForm("Create", new AjaxOptions { OnComplete = "Add_OnComplete"}))
    {
     <fieldset>
        <legend>Add</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.FullName)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.FullName)
            @Html.ValidationMessageFor(model => model.FullName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.PhoneNumber)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.PhoneNumber)
            @Html.ValidationMessageFor(model => model.PhoneNumber)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Status)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Status)
            @Html.ValidationMessageFor(model => model.Status)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
} 


     <script type="text/javascript">
         function Add_OnComplete(context) {

             var JsonAdd = context.get_response().get_object();

             if (JsonAdd.Success) {
                //TODO
             }

         }
    </script>

Контроллер

 public PartialViewResult Create()
    {
        return PartialView();
    }

     [HttpPost]
    public JsonResult Create(Subscribe model)
    {
        if (ModelState.IsValid)
        {

             _entity.CreateSubscribe(model, SubscribeStatus.Customer.GetHashCode());
             var   message = new Message(MailSubject, MailBody, model.Email);

            message.Send();
        }

        return Json(new
        {
            Success = true,
            Message = "The person has been added!"
        });
    }

Что я делаю не так? И чего мне не хватает?

Спасибо.

1 Ответ

1 голос
/ 07 февраля 2012

Удалите этот метод get_response (). Get_object ().

Использовать только контекст. Успех

...