首页 > 解决方案 > View 中的 Asp.net core MVC 模型更新

问题描述

我有我的模型“模型”:

public class model
    {
        /// Tady přidat všechny kolonky v db a jejich vlastnosti pomocí [VLASTNOST]
        [Key]
        public int Id { get; set; }
        public string Data1 { get; set; }
        public string Data2 { get; set; }
        public string Data3 { get; set; }
        public string Data4 { get; set; }
   }

然后我的控制器:

[HttpGet]
        public async Task<IActionResult> View(int? id)
        {
            ViewData["Reklamace"] = await _db.model.ToListAsync();
            ViewBag.Id = id ?? 1;
            return View();
        }

然后我的观点:

 IEnumerable<model> ReklamaceModel = ViewData["model"] as IEnumerable<model>;
    int Pozice = (int)ViewBag.Id;
    int previous = 1;
    if (Pozice != 1)
    {
        previous = Pozice - 1;
    }
    int next = Pozice + 1;
    int last = ReklamaceModel.Count();
    int first = 1;
    if (next > last)
    {
        next = Pozice;
    }
    if (first > Pozice)
    {
        Pozice = first;
    }
    int IdPozice = 0;
    try
    {
        IdPozice = ReklamaceModel.ElementAt(Pozice).Id;
    }
    catch
    {
        IdPozice = ReklamaceModel.ElementAt(Pozice - 1).Id;
    }
   var MainModel = ReklamaceModel.Where(s => s.Id == IdPozice).First();

然后我需要在@Html.TextBoxFor(). 我试图在后端进行这些计算和模型操作,但我需要加载速度,当我在后端进行时,我的网络速度要慢 3 倍。所以我得出的结论是,我需要以某种方式在 View 中将我的 @model 设置为 null 模型,然后更新它的值。一些想法如何做到这一点?我试过@Html.TextBoxFor(m => m.Data1, new { @Value = "3" }) 为什么我需要这个?:因为我正在尝试插入数据并且我发现这@Html.TextBoxFor在某种程度上最适合它(使用实体框架)

标签: c#asp.net-mvcentity-frameworkasp.net-coreasp.net-core-mvc

解决方案


为什么要发送所有数据?请求是针对特定 ID。在控制器中试试这个:

ViewData["Reklamace"] = await _db.model.Find(id);


推荐阅读