【问题标题】:MVC reusing modelMVC 复用模型
【发布时间】:2018-10-22 11:20:57
【问题描述】:

我有一个 MVC Web 应用程序,它使用表单从用户那里获取邮政编码,然后查询外部 Web 服务并向视图返回地址列表。

我之前遇到过一个问题,即我的模型是静态的,因此数据会显示给其他客户端上的用户,而不是特定于每个用户。我现在已经得到它,因此每个单独的用户都可以在视图上查询并获取他们的地址列表(其他人看不到)但是,当用户刷新页面或返回时,数据会丢失。

我在页面刷新时有一些代码检查数据是否存在并且不会将用户带到表单的开头,但是在页面刷新时模型返回 null,因此它总是会将它们带回开始。

有什么想法吗?理想情况下,我希望能够为当前用户多次使用数据,但如果他们刷新并通过表单说 90%,他们将丢失整个数据。看起来应该很容易,但我尝试过的所有示例都不适用于我的特定场景。

控制器:

public class AssistedController : Controller
{
    // GET: Assisted
    AddressList model;


    public ActionResult Index()
    {
      return View(model);
    }

    [HttpPost]
    public ActionResult GetAddresses(string postcode)
    {
        model = new AddressList();
        if (postcode == null || postcode == "")
        {
            return RedirectToAction("/Index/");
        }
        //call enviroweb web service
        AddressWeb ew = new AddressWeb();
        //extract address values from the XML returned from web service
        XmlNode xml = ew.GetAddress(", , , , " + postcode);

        foreach (XmlElement addressInfo in xml)
        {
            foreach (XmlElement teset in addressInfo["Addresses"])
            {
                //add each address item found to the list
                model.listone.Add(new AddressResults {
                    FullAddress = teset["fulladdress"].InnerText,
                    Lat = teset["Lat"].InnerText,
                    Lon = teset["Long"].InnerText,
                    addLine1 = teset["addline1"].InnerText,
                    addLine2 = teset["addline2"].InnerText,
                    addLine3 = teset["addline3"].InnerText,
                    addLine4 = teset["addline4"].InnerText,
                    Town = teset["Town"].InnerText,
                    postcode = teset["postcode"].InnerText,
                    Ownership = teset["Ownership"].InnerText,
                    WeekNumber = teset["WeekNumber"].InnerText

                });
            }
        }

        //return the list and model back to the index view
        return View("Index", model);


    }

查看:

<!--Use the model to return the data-->
@model AddressSearch.Models.AddressList
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

@if (Model == null)
{

}
  else
{
if (Model.listone.Count != 0)
  {

//this section returns the items as options in the select if the list count is greater than 0.
foreach (var test in Model.listone)
 {
 <option value="@test.FullAddress">@test.FullAddress</option>
                                }



                            }
                        }

型号:

public class AddressList
{
    public List<AddressResults> listone = new List<AddressResults>();
}

【问题讨论】:

  • 如果用户刷新页面,那么他们正在调用返回空AddressList的GET方法。您可以对接受 string postcode 作为参数的方法进行 GET,而不是进行 POST(或者只需将 Index() 方法更改为接受 string postcode
  • 一旦用户最初输入了他们的邮政编码并进行了搜索,网络服务就会将该数据存储在“地址列表”模型中。有没有办法让地址列表保持填充而不是保持邮政编码?这意味着如果用户刷新页面,则必须重新调用 Web 服务来获取地址信息。
  • Web 是无状态的 - 您的 AddressList 模型在每个请求中都会再次初始化,所以除非您将结果存储在其他地方(例如 SessionMemoryCache 等),否则您将首先检查在调用 Web 服务之前查看结果。
  • 我使用了一个会话,它已经完成了它应该做的事情。谢谢

标签: c# asp.net-mvc web-applications


【解决方案1】:

ASP.NET MVC 中的TempData 可用于存储临时数据,可用于后续请求。 TempData 将在后续请求完成后被清除。

public class AssistedController : Controller
{
    // GET: Assisted
    AddressList model;


    public ActionResult Index()
    {
        if (TemData.ContainsKey("address"))
        {
            model = TempData["address"] as AddressList;
        }
        return View(model);
    }

    [HttpPost]
    public ActionResult GetAddresses(string postcode)
    {
        model = new AddressList();
        if (postcode == null || postcode == "")
        {
            return RedirectToAction("/Index/");
        }
        if (TemData.ContainsKey("address"))
        {
            model = TempData["address"] as AddressList;
            return View(model);
        }
        //call enviroweb web service
        AddressWeb ew = new AddressWeb();
        //extract address values from the XML returned from web service
        XmlNode xml = ew.GetAddress(", , , , " + postcode);

        foreach (XmlElement addressInfo in xml)
        {
            foreach (XmlElement teset in addressInfo["Addresses"])
            {
                //add each address item found to the list
                model.listone.Add(new AddressResults
                {
                    FullAddress = teset["fulladdress"].InnerText,
                    Lat = teset["Lat"].InnerText,
                    Lon = teset["Long"].InnerText,
                    addLine1 = teset["addline1"].InnerText,
                    addLine2 = teset["addline2"].InnerText,
                    addLine3 = teset["addline3"].InnerText,
                    addLine4 = teset["addline4"].InnerText,
                    Town = teset["Town"].InnerText,
                    postcode = teset["postcode"].InnerText,
                    Ownership = teset["Ownership"].InnerText,
                    WeekNumber = teset["WeekNumber"].InnerText

                });
            }
        }
        TempData["address"] = model;
        //return the list and model back to the index view
        return View("Index", model);
    }
}

使用方法参考this link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    相关资源
    最近更新 更多