【问题标题】:ASP.NET MVC Partial View issue when Submitting a Form提交表单时的 ASP.NET MVC 部分视图问题
【发布时间】:2014-01-05 19:53:08
【问题描述】:

我正在尝试在主(索引)视图中显示部分视图:

步骤:

  1. 用户从索引视图中选择一个下拉项目。
    • 这会显示一个包含搜索表单的部分视图。
  2. 用户填写搜索表单,然后单击提交按钮。
  3. 如果搜索表单有效,则会显示一个新页面(结果视图)。
  4. 否则,搜索表单部分视图应在主视图内重新显示错误

我遇到了数字 4 的问题,因为当搜索表单提交时,它仅在新窗口中显示
部分视图。我想显示整个页面:Index View + Partial View with errors.

建议?这就是我所拥有的:

图片

控制器

public class AppController : Controller
{
    public ActionResult Index()
    {
        return View(new AppModel());
    }

    public ActionResult Form(string type)
    {
        if (type == "IOS")
            return PartialView("_IOSApp", new AppModel());
        else
            return PartialView("_AndroidApp", new AppModel());
    }

    public ActionResult Search(AppModel appModel)
    {
        if (ModelState.IsValid)
        {
            return View("Result");
        }
        else // This is where I need help
        {
            if (appModel.Platform == "IOS")
                return PartialView("_IOSApp", appModel);
            else
                return PartialView("_AndroidApp", appModel);
        }
    }
}

型号

public class AppModel
{
    public string Platform { get; set; }
    [Required]
    public string IOSAppName { get; set; }
    [Required]
    public string AndroidAppName { get; set; }        
    public List<SelectListItem> Options { get; set; }

    public AppModel()
    {
        Initialize();
    }

    public void Initialize()
    {
        Options = new List<SelectListItem>();

        Options.Add(new SelectListItem { Text = "IOS", Value = "I" });
        Options.Add(new SelectListItem { Text = "Android", Value = "A"});
    }
}

索引.cshtml

@{ ViewBag.Title = "App Selection"; }

<h2>App Selection</h2>

@Html.Label("Select Type:")
@Html.DropDownListFor(x => x.Platform, Model.Options)

<div id="AppForm"></div> // This is where the Partial View goes

_IOSApp.cshtml

@using (Html.BeginForm("Search", "App"))
{
    @Html.Label("App Name:")
    @Html.TextBoxFor(x => x.IOSAppName)
    <input id="btnIOS" type="submit" value="Search IOS App" />
}

AppSearch.js

$(document).ready(function () {

$("#Platform").change(function () {

    value = $("#Platform :selected").text();

    $.ajax({
        url: "/App/Form",
        data: { "type": value },
        success: function (data) {
            $("#AppForm").html(data);
        }
    })
});

});

【问题讨论】:

  • 为什么不使用@Ajax.BeginForm(...)
  • 这就是我正在使用的 - 看看我的 _IOSAPP.cshtml。但是,当表单提交时,它会转到 Search 操作方法,如果模型无效,它只会显示部分视图。我想要的是整个页面(索引视图 + 有错误的部分视图)
  • 我的意思是使用 Ajax.BeginForm 而不是 Html.BeginForm,所以提交时,转到搜索操作,如果有效,返回部分视图,搜索结果更新到页面,如果无效,返回部分视图错误消息更新到页面。sumbit 请求是一个 ajax 发布请求

标签: c# asp.net-mvc asp.net-mvc-4 razor asp.net-mvc-partialview


【解决方案1】:

你也需要通过ajax调用搜索方法。

更改 index.html 然后

1- 如果表单有效,则替换整个 html 或 mainContainer(我添加到视图中的 div)。

2- 否则只替换部分视图。

@{ ViewBag.Title = "App Selection"; }
<div id="mainContainer">
<h2>App Selection</h2>

@Html.Label("Select Type:")
@Html.DropDownListFor(x => x.Platform, Model.Options)

<div id="AppForm"></div> // This is where the Partial View goes
</div>

从您的局部视图中删除表单标签,只需调用一个 ajax 调用方法进行搜索。 处理此问题的最简单方法可能是使用 MVC unobtrusive ajax

【讨论】:

  • 这个 ajax 只在下拉选择更改时执行(它只是用来显示表单) - 我没有问题。我想要的是:单击提交按钮时-> 调用“搜索”操作方法。在这里,如何显示带有错误的局部视图?
  • 添加一个 div 用于在局部视图或您的主视图中显示错误消息,并在您的 Ajax 调用中出现错误时显示它。
  • 此 Ajax 将转到 /App/Form,当模型无效时,我需要帮助管理 /App/Search 中的返回。
【解决方案2】:

我会说使用内联 Ajax 来提交此表单。

@using (Html.BeginForm("Search", "App"))
{
   @Html.Label("App Name:")
   @Html.TextBoxFor(x => x.IOSAppName)

   <input id="btnIOS" type="submit" value="Search IOS App" />
}

把上面给定的代码改成下面的代码

@using (

Ajax.BeginForm(
    "Form", "App",
    new AjaxOptions()
    {
        UpdateTargetId = "App",
        HttpMethod = "Post"
    }
   ) 
 )

 {   
    <div class="editor-label">
        @Html.Label("App Name:")
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(x => x.IOSAppName)
    </div>
    <input id="btnIOS" type="submit" value="Search IOS App" />

 }

 //in controller change the parameter of the given method from string type to model object which will be posted by ajax form.
  public ActionResult Form(AppModel appModel)
  {
    if (appModel.type == "IOS")
        return PartialView("_IOSApp", new AppModel());
    else
        return PartialView("_AndroidApp", new AppModel());
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多