我没有正确发布数据吗?

发布于 2024-10-12 18:36:55 字数 3067 浏览 1 评论 0原文

我不确定我是否从 ASP.NET MVC 应用程序正确发布了多个部分页面。

在我的网站上,我加载了许多部分页面并将它们显示在 jQuery UI 选项卡中。下面是我的 Index.aspx 页面中的示例(人为的示例):

<div id="tabScenario"><% Html.RenderPartial("Scenario", Model); %></div>
<div id="tabPerson"><% Html.RenderPartial("Person", Model.People.FirstOrDefault()); %></div>
<div id="tabAddress"><% Html.RenderPartial("Address", Model.People.FirstOrDefault().Addresses.FirstOrDefault()); %></div>

我的部分视图都是强类型化的,这些对象中的每个对象(场景、人员和地址)的单一版本在这种情况下)。

用户输入他或她想要更改的数据,然后保存数据。当我发布该数据时,我在控制器中执行此操作:

[HttpPost]
[Header("Setup Scenario")]
public ActionResult Index(Scenario scenario, Person person, Address address, string submitButton)
{
    // Update the scenario with all the information that belongs to it.
    scenario.Person = person;
    scenario.Person.Address = address;

    // Determine whether to just save or to save and submit.
    switch (submitButton)
    {
        case "Save":
            return Save(scenario, true);
        case "Save As...":
            return Save(scenario, false);
        case "Submit":
            return Submit(scenario);
        default:
            return View();
    }
}

我不完全确定这有多正确,因为当我去显示我刚刚在下一个视图上发布的信息时,我在线收到以下运行时错误:

<div id="tabPerson"><% Html.RenderPartial("Person", Model.People.FirstOrDefault()); %></div>

错误:

模型项传入 字典的类型是 'Mdt.ScenarioDBModels.Scenario',但是 这本词典需要一个模型项目 类型为“Mdt.ScenarioDBModels.Person”。

让我困惑的是,如果你看一下特定的行,我得到的是 Person。因此,根据这篇文章,它告诉我我的值很可能为 null,并且 ASP.NET 正在“回退”到 Scenario 对象。

由于这一切,我认为我在发布所有数据(有很多)的方式上做了一些不正确的事情,但我坚持到底会是什么。

澄清

我通过 Ajax 发帖。这是 BeginForm 语句。

<% using (Ajax.BeginForm("Index", "Scenario", new AjaxOptions { HttpMethod = "Post", OnSuccess = "scenarioSubmitSuccess" }, new { id = "scenarioForm" }))
{ %>
   // My Index.aspx
<% } %>

Save 方法基本上是尝试将模型保存到后备存储(在本例中为数据库)。方法如下:

    /// <summary>
    /// Save a the scenario.
    /// </summary>
    /// <param name="scenario">The scenario to save to the database.</param>
    /// <param name="overwrite">True if the existing scenario should be updated in the database.</param>
    /// <returns></returns>
    private ActionResult Save(Scenario scenario, bool overwrite)
    {
        if (ModelState.IsValid && TryUpdateModel(scenario, "Scenario"))
        {
            ScenarioDBEntities entities = ObjectContextFactory.GetScenarioDBEntities();
            ScenarioRepository scenarioRepository = new ScenarioRepository(entities);

            if (overwrite)
            {
                scenarioRepository.Update(scenario);
            }
            else
            {
                scenarioRepository.Add(scenario);
            }

            entities.SaveChanges();
        }

        return View(scenario);
    }

I am not sure if I am posting multiple partial pages correctly from my ASP.NET MVC application.

On my site, I load a number of partial pages and display them in jQuery UI tabs. Here is an example of how that looks in my Index.aspx page (contrived example):

<div id="tabScenario"><% Html.RenderPartial("Scenario", Model); %></div>
<div id="tabPerson"><% Html.RenderPartial("Person", Model.People.FirstOrDefault()); %></div>
<div id="tabAddress"><% Html.RenderPartial("Address", Model.People.FirstOrDefault().Addresses.FirstOrDefault()); %></div>

My partial views are all strongly-typed to the singular version of each of those objects (Scenario, Person, and Address in this case).

The user enters the data he or she wants to change and then saves the data. When I post that data, I am doing this in my controller:

[HttpPost]
[Header("Setup Scenario")]
public ActionResult Index(Scenario scenario, Person person, Address address, string submitButton)
{
    // Update the scenario with all the information that belongs to it.
    scenario.Person = person;
    scenario.Person.Address = address;

    // Determine whether to just save or to save and submit.
    switch (submitButton)
    {
        case "Save":
            return Save(scenario, true);
        case "Save As...":
            return Save(scenario, false);
        case "Submit":
            return Submit(scenario);
        default:
            return View();
    }
}

I'm not entirely sure how correct this is because, when I go to display the information I just posted on the next view, I am receiving the following runtime error on line:

<div id="tabPerson"><% Html.RenderPartial("Person", Model.People.FirstOrDefault()); %></div>

Error:

The model item passed into the
dictionary is of type
'Mdt.ScenarioDBModels.Scenario', but
this dictionary requires a model item
of type 'Mdt.ScenarioDBModels.Person'.

What confuses me is that, if you look at the particular line, I am getting Person. So, based off of this post, it tells me my value is most likely null and ASP.NET is "falling back" to the Scenario object.

Because of all this, I figured I am doing something incorrect in how I am posting all my data (there is a lot of it), but I am stuck at what that would be.

Clarifications

I am posting via Ajax. Here is the BeginForm statement.

<% using (Ajax.BeginForm("Index", "Scenario", new AjaxOptions { HttpMethod = "Post", OnSuccess = "scenarioSubmitSuccess" }, new { id = "scenarioForm" }))
{ %>
   // My Index.aspx
<% } %>

The Save method is, basically, trying to save the model to the backing store (a database in this case). Here is the method:

    /// <summary>
    /// Save a the scenario.
    /// </summary>
    /// <param name="scenario">The scenario to save to the database.</param>
    /// <param name="overwrite">True if the existing scenario should be updated in the database.</param>
    /// <returns></returns>
    private ActionResult Save(Scenario scenario, bool overwrite)
    {
        if (ModelState.IsValid && TryUpdateModel(scenario, "Scenario"))
        {
            ScenarioDBEntities entities = ObjectContextFactory.GetScenarioDBEntities();
            ScenarioRepository scenarioRepository = new ScenarioRepository(entities);

            if (overwrite)
            {
                scenarioRepository.Update(scenario);
            }
            else
            {
                scenarioRepository.Add(scenario);
            }

            entities.SaveChanges();
        }

        return View(scenario);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

り繁华旳梦境 2024-10-19 18:36:56

中调用此方法(场景、人员或地址):

return View(scenario);

问题是,在您的 Save 方法中,您始终将场景作为视图模型传递,无论在哪个上下文 您的 Person.ascx 部分需要 Person 作为视图模型。因此,如果您尝试更新人员部分,则需要将人员传递到视图。

The problem is that in your Save method you are always passing scenario as view model, no matter in which context this method has been invoked (scenario, person or address):

return View(scenario);

while your Person.ascx partial expects Person as view model. So you need to pass the person to the view in case you are trying to update the person partial.

离鸿 2024-10-19 18:36:56

事实证明,我没有正确更新模型。答案可以在这里找到:为什么更新对象仅以一种特定方式工作?

Turns out, I wasn't updating the model properly. The answer can be found here: Why does updating an object only work one particular way?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文