weixin_33724046 2014-09-02 15:37 采纳率: 0%
浏览 36

防止在MVC中进行Ajax调用

Hi i am making my project in the asp.net,

basic expected behaviour -- fill form name , select master module(dropdown), select sub modules(dropdown), ajax passes id of submodule dropdown, create(submit).. it will submit all values,

now code is behaves---- fill form name, select master and submodule, while selecting submodule from second dropdown is calling the ajax call, and create action is called, so the form name and masterID(that is extracted from first dropdown) gone blank... so i need to prevent the ajax call to call the controller

Myform in razor view

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Form</legend>

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

        <select id="State" name="state"></select><br />
        <p>
            <input id="sbmt" type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

My ajax call

 $('#State').change(function () {
            var a = $('#State').val();
            var token = $('[name=__RequestVerificationToken]').val();
            $.ajax({
                url: "/form/create",
                type: "POST",
                data: { __RequestVerificationToken: token, 'SubID': a }
            });

        });

My controller

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Form form,  int SubID)
        {
            if (ModelState.IsValid)
            {
                form.CreatedBy = 1;
                form.SubId = SubID;
               form.CreatedDate = DateTime.Now;
                form.ModifyBy = 1;
                form.ModifyDate = DateTime.Now;
                form.IsActive = true;
                form.IsDeleted = false;
                db.Forms.Add(form);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.MasterID = new SelectList(db.Departments, "MasterId", "ModuleName", form.MasterID);
            return View(form);
        }
  • 写回答

2条回答 默认 最新

  • 狐狸.fox 2014-09-02 16:29
    关注

    I guess the easiest way for you would be to use MVC's ajax helpers. Change your form like so:

    @using (Ajax.BeginForm("Create", "Home", null, new AjaxOptions { OnSuccess = "OnSuccess" }))
    {
    ...
    

    And afterwards add a javascript handler for onsuccess

    function OnSuccess(){
       alert("success");
    }
    

    This is barely functional code just to get you started.

    PS. Make sure you add a reference to ~/Scripts/jquery.unobtrusive-ajax.min.js

    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?