c# - 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
在某种程度上最适合它(使用实体框架)
解决方案
为什么要发送所有数据?请求是针对特定 ID。在控制器中试试这个:
ViewData["Reklamace"] = await _db.model.Find(id);
推荐阅读
- javascript - 当我们进行类型转换时,除了 Object 类型之外,任何数据类型都可以转换为字符串?为什么对象具有这种性质?
- package - 如何在包/包主体声明后使用“std_logic”?
- protractor - 量角器 - 选择会选择所有选项,脚本无法正确选择其中一个
- c# - 如何使用 Monogame 在游戏中创建简单场景?
- flask-testing - 如何使用 flask_testing 测试 POST 请求
- c# - SemaphoreSlim 不对并发请求进行排队,仍然允许双重预订
- javascript - 当目录(FileWatcher)发生变化时如何输出警报消息?
- haskell - 了解 Haskell 中的递归斐波那契函数
- karate - 缺少版本 0.9.3 和 0.9.4 的依赖项
- python - 在列中具有重复值的熊猫数据框的条件旋转