简体   繁体   中英

Ajax.beginForms MVC Partial View

I am using an ajax.beginform to create a partial view within another view.

I the user enters a correct sn everything works fine. But if the user enters an invalid number, I want to redirect to the index view.

Now the index page is submitted as a partial view in itself.

How can I avoid that.

Here is a part of my view and 2 simplified actionresults.

 @using (Ajax.BeginForm("MachineInfo", "QrCreate", new AjaxOptions() { 
 HttpMethod = "POST", UpdateTargetId = "form-content", InsertionMode = 
 InsertionMode.ReplaceWith }))
 {
            @Html.AntiForgeryToken()

            <input type="text" id="sn" name="sn" class="inputsn" 
              placeholder="Enter your serial number here..." />

            <input type="submit" value="Search" class="search btn btn-success btn-lg" />

 }
    </div>

</div>
<div id="form-content"></div>

my Controller

  public ActionResult Index(bool? isValidMachine = null)
    {
        ViewBag.invalidSerialNumber = isValidMachine;
        return View();
    }
    [HttpPost]
    public ActionResult MachineInfo(string sn)
    {

        if(string.IsNullOrEmpty(sn))
        RedirectToAction("Index", new { isValidMachine = false });


       QrCreateViewModel qrCreateVM;
       using (var machineService = new MachineApiService())
        {

            var machine = machineService.GetMachineFromSerialNumber(sn);
            if (machine == null)
                return RedirectToAction("Index", new { isValidMachine = false });
            else
            qrCreateVM = new QrCreateViewModel(machine, GetBasePath());
        }

       if (qrCreateVM.IsValid())
       {
           qrCreateVM.Viewurl = qrCreateVM.QrCreateUrlOrDefaultNull();
           return PartialView(qrCreateVM);
       }

       else
         return  RedirectToAction("Index", new { isValidMachine = false });
    }

Ajax calls do not redirect (the purpose of making them is to stay on the same page).

In your controller method, replace the instances of return RedirectToAction(...) to return a HttpStatusCodeResult indicating an error, which you can then handle in the OnFailure option to redirect to the Index() method.

For example

[HttpPost]
public ActionResult MachineInfo(string sn)
{

    if (string.IsNullOrEmpty(sn))
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Bad Request");
    }
    ....

Then in the Ajax.BeginForm()

@using (Ajax.BeginForm("MachineInfo", "QrCreate", new AjaxOptions() { 
    HttpMethod = "POST", 
    UpdateTargetId = "form-content",
    InsertionMode = InsertionMode.ReplaceWith,
    OnFailure = "redirect"
}))
{
    ....

and add the following script to redirect

function redirect(ajaxContext) {
    location.href = '@Url.Action("Index")';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM