簡體   English   中英

Asp.net 內核 MVC model 更新查看

[英]Asp.net core MVC model update in View

我有我的 model “型號”:

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; }
   }

然后我的 controller:

[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()中使用類似數據源的“MainModel”。 我曾嘗試在后端進行這些計算和 model 操作,但我需要加載速度,當我在后端執行此操作時,我的 web 慢了 3 倍。 所以我得出的結論是,我需要以某種方式在 View my @model 中設置為 null model 然后更新它的值。 一些想法如何做到這一點? 我已經嘗試過@Html.TextBoxFor(m => m.Data1, new { @Value = "3" })為什么我需要這個?:因為我正在嘗試插入數據並且我發現@Html.TextBoxFor在某種程度上是最好的為它(使用實體框架)

為什么要發送所有數據? 請求是針對特定 ID。 在 controller 中試試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM