代码之家  ›  专栏  ›  技术社区  ›  Château Palmer

ajaxBeginForm不插入局部视图

  •  0
  • Château Palmer  · 技术社区  · 6 年前

    正在运行ASP。2015年净MVC,含C#。 由于@使用(Ajax.BeginForm(…),正在尝试添加部分视图 它所做的就是打开一个新页面,其中包含我的部分视图。我在stackoverflow中找到了一些答案,但没有一个对我有用,除非我遗漏了什么。

    场景: index page here 类型A、B或C,部分视图应位于当前页面下方

    new page unexpected here 但它确实打开了一个新的页面

    控制器 :

    public class TestController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        [ChildActionOnly]
        public ActionResult TestPartial()
        {
            ChoiceViewModel vm = new ChoiceViewModel();
            return View(vm);
        }
    
        [HttpPost]
        public ActionResult SelectChoice(ChoiceViewModel vm)
        {
            ModelState.Clear();
            if (vm.MyChoice == "A")
                return PartialView("APartial");
    
            if (vm.MyChoice == "B")
                return PartialView("BPartial");
    
            if (vm.MyChoice == "C")
                return PartialView("CPartial");
    
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
    }
    

    指数cshtml:

    <h2>Index</h2>
    <div>
       @Html.Partial("TestPartial")
    </div>
    <br/>
    <div id="GoesHere">
    </div>
    

    测试部分。cshtml:

    @model TestPartial.ViewModels.ChoiceViewModel
    
    @using (Ajax.BeginForm("SelectChoice", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "GoesHere", InsertionMode = InsertionMode.Replace }))
    {
       <div class="form-group">
           <p>Give your choice please: A,B or C</p>
           @Html.TextBoxFor(x => x.MyChoice, new { @class = "form-control" })
       </div>
       <div class="form-group">
           <input type="submit" class="btn btn-primary" value="Select">
       </div>
    }
    

    部分。cshtml (以及B和C…)

    <p>This is A choice</p>
    

    备注:

    • 是的,网络。config具有UnobtrusiveJavaScriptEnabled=true和ClientValidationEnabled=true
    • 是的,我安装了jquery。不显眼的ajax包

    你能告诉我我错过了什么或做错了什么吗? 谢谢

    1 回复  |  直到 6 年前
        1
  •  0
  •   Château Palmer    6 年前

    答案是,我在cshtml中缺少了不显眼的ajax包含行:

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