gpt4 book ai didi

c# - MVC 绑定(bind)到实体调用区域

转载 作者:行者123 更新时间:2023-11-30 12:20:08 26 4
gpt4 key购买 nike

我有一个名为 Area 的数据库实体。在我的 Create 模型中,我有以下代码:

public Entities.Entities.Area Area { get; set; }

然后在我看来,我只为 Area 中的每个属性输入了一个输入。

@Html.TextBoxFor(x => x.Area.Name)

当我保存表单时,Area 对象被传回为 null。但是,当我在 Create 模型中更改 Area 对象名称并更新 View 中的引用时,它会保存。

public Entities.Entities.Area AreaDetail { get; set; }

@Html.TextBoxFor(x => x.AreaDetail.Name)

为什么当 Area 对象被简单地命名为 Area 时绑定(bind)会失败?它与MVC区域有关吗?如果是这样,那将如何弄乱绑定(bind)?

最佳答案

我对类名 AREA 做了同样的事情,它工作得很好,这是代码

获取方法

[HttpGet]
public ActionResult ByReleasedDate()
{
return View();
}

POST方法

[HttpPost]
public ActionResult ByReleasedDate(CreateModel model)
{
return Content($"Area name is {model.Area.Name}");
}

创建模型

public class CreateModel
{
public Area Area { get; set; }
}

区域模型

public class Area
{
public string Name { get; set; }
}

post方法证明 Proof for post method

关于c# - MVC 绑定(bind)到实体调用区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53259179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com