【问题标题】:ASP .net MVC List not updatingASP .net MVC 列表未更新
【发布时间】:2016-01-27 03:22:01
【问题描述】:

我在类中设置了列表,该列表已初始化并在主构造函数中添加 1 项,但由于某种原因,从创建视图添加它不会将其添加到列表中。任何帮助将不胜感激。

public class PhoneBase
{
    public PhoneBase()
    {
        DateReleased = DateTime.Now;
        PhoneName = string.Empty;
        Manufacturer = string.Empty;
    }
    public int Id { get; set; }
    public string PhoneName { get; set; }
    public string Manufacturer { get; set; }
    public DateTime DateReleased { get; set; }
    public int MSRP { get; set; }
    public double ScreenSize { get; set; }
}

public class PhonesController : Controller
{
    private List<PhoneBase> Phones;
    public PhonesController()
    {
        Phones = new List<PhoneBase>();
        var priv = new PhoneBase();
        priv.Id = 1;
        priv.PhoneName = "Priv";
        priv.Manufacturer = "BlackBerry";
        priv.DateReleased = new DateTime(2015, 11, 6);
        priv.MSRP = 799;
        priv.ScreenSize = 5.43;
        Phones.Add(priv);
    }

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

    // GET: Phones/Details/5
    public ActionResult Details(int id)
    {
        return View(Phones[id - 1]);
    }

这里我通过使用 formcollections 创建视图插入新的列表项

public ActionResult Create()
    {
        return View(new PhoneBase());
    }

    // POST: Phones/Create
    [HttpPost]
public ActionResult Create(FormCollection collection)
    {
        try
        {
            // TODO: Add insert logic here
            // configure the numbers; they come into the method as strings
            int msrp;
            double ss;
            bool isNumber;

            // MSRP first...
            isNumber = Int32.TryParse(collection["MSRP"], out msrp);
            // next, the screensize...
            isNumber = double.TryParse(collection["ScreenSize"], out ss);

            // var newItem = new PhoneBase();
            Phones.Add(new PhoneBase
            {
                // configure the unique identifier
                Id = Phones.Count + 1,

                // configure the string properties
                PhoneName = collection["PhoneName"],
                Manufacturer = collection["manufacturer"],

                // configure the date; it comes into the method as a string
                DateReleased = Convert.ToDateTime(collection["DateReleased"]),

                MSRP = msrp,
                ScreenSize = ss
            });


            //show results. using the existing Details view
            return View("Details", Phones[Phones.Count - 1]);
        }
        catch
        {
            return View();
        }
    }

查看整个列表不会显示通过创建视图添加的任何项目。

【问题讨论】:

  • 您如何查看整个列表?是否有单独的操作方法?如果是,那看起来如何?如果不是,并且它是详细信息视图,则您正在将单个项目从电话列表传递到视图。您希望它如何显示所有项目?
  • @Shyju 我已经添加了大部分内容。我认为问题出在创建视图操作方法中,这就是不包括在内的原因。我现在已经更新了。
  • 您需要将手机存储在数据库之类的地方。

标签: c# asp.net asp.net-mvc formcollection


【解决方案1】:

因为HTTP 是无状态的!Phones 是您的 PhonesController 中的一个变量,并且每个对该控制器的 http 请求(对其各种操作方法)都会创建此类的一个新实例,从而再次重新创建 Phones 变量,执行构造函数代码,将一个Phone项添加到这个集合中。

您在创建操作中添加到 Phones 集合的项目在下一个 http 请求(对于 Phones/Index)中不可用,因为它不知道上一个 http 请求做了什么。

您需要保持数据在多个请求之间可用。您可以将其存储在数据库表/XML 文件/临时存储(如 Session 等)中。

【讨论】:

  • 为什么不展示解决方案?你并没有真正帮助他。
  • @JohnPeters,解决方案在答案的最后一段。
猜你喜欢
  • 2017-09-16
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
相关资源
最近更新 更多