使用MVC和Razor的Ajax BeginForm POST

时间:2016-11-21 08:30:38

标签: asp.net-mvc razor asp.net-ajax

我一直在搜索404并搜索所有搜索引擎优化,但无法在此处找到问题。表单是渲染操作的结果,并显示在主页(主控制器)上。但是,我希望它发布一个不同的控制器动作,它不断给我一个404.我已经包含了所有正确的脚本,不显眼的javascript以及必要的web.config设置,我无法遇到类似的问题来自我的研究。

这是正在呈现的表单的部分:

@model AFS.Models.SearchLocationModel
<div class="site-search-module">
<div class="site-search-module-inside">
    @using (Ajax.BeginForm("SearchCare", "LevelOfCare", null, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "searchDiv" }, new { @class = "search-form", enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        <div class="row">
            <div class="col-md-12">
                <h5>Select a category</h5>
                @Html.DropDownListFor(x => x.Level, Model.LevelSelectList, new { @class = "form-control input-lg selectpicker" })
            </div>
            <div class="col-md-12">
                <h5>Enter location</h5>
                <input type="text" id="Location" name="Location" class="form-control input-lg selectpicker" placeholder="City, State OR Zip Code" required />
            </div>
            <div class="col-md-12"> <button type="submit" class="btn btn-primary btn-block btn-lg search"><i class="fa fa-search"></i> Search</button> </div>
        </div>
    }
</div>

控制器操作是:

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult SearchCare(SearchLocationModel model)
    {
        if (ModelState.IsValid)
        {


            SearchLocationModel geocodeModel = Geocode(new SearchLocationModel() { Level = model.Level, Location = model.Location });
            if (geocodeModel.Status == "OK")
            {
                Session["level"] = model.Level;
                return RedirectToRoute("LevelCity", new { level = model.Level, state = geocodeModel.State, city = geocodeModel.City, latitude = geocodeModel.Latitude, longitude = geocodeModel.Longitude });
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Please enter City, State OR Zip Code.");
                return RedirectToAction("SearchWidget", "Home");
            }
        }
        else
        {
            return RedirectToAction("SearchError");
        }
    }

0 个答案:

没有答案