MVC重用模型

时间:2018-10-22 11:20:57

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

我有一个MVC Web应用程序,该应用程序使用表单从用户那里获取邮政编码,然后该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>();
}

1 个答案:

答案 0 :(得分:0)

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使用方法