我是新来的,试图弄清楚发生了什么,并认为这是最好的询问场所。好吧,当我选择项目并按下添加按钮时,属性[Bind“”] Model变为null,但是为什么呢?
这是我的观点:
@model PortfolioDetailsVM
<form asp-controller="Portfolio" asp-action="AddProject" method="POST">
<div class="form-group">
<div class="input-group mb-3">
<select asp-for="PortfolioProjects.ProjectId" class="custom-select form-control">
<option disabled selected value="@null">Choose...</option>
@foreach (var item in Model.Projects)
{
<option value="@item.ProjectID">@item.Title</option>
}
</select>
var PortfolioVM的所有数据都为空。 这是我的控制器和我的视图模型:
namespace PEG.Models
{
public class PortfolioDetailsVM
{
public PortfolioDetailsVM()
{
Portfolios = new Portfolio();
PortfolioProjects = new PortfolioProject();
}
public Portfolio Portfolio;
public PortfolioProject PortfolioProjects;
public IEnumerable<Project> Projects;
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddProject([Bind("PortfolioProject")]PortfolioDetailsVM PortfolioVM) //<----Null
{
var addproject = PortfolioVM.PortfolioProjects;
if (ModelState.IsValid)
{
try
{
context.Update(addproject);
await context.SaveChangesAsync();
return RedirectToAction("Details", "Portfolio" + PortfolioVM.PortfolioProjects.PortfolioId);
}
catch (DbUpdateException)
{
ModelState.AddModelError("", "Unable to save changes. " +
"Try again, and if the problem persists, " +
"see your system administrator.");
}
}
return RedirectToAction("Index", "Portfolio");
}
这是另一个模型和详细信息方法:
// GET: Portfolio/Details/5
public async Task<ActionResult> Details(int id)
{
PortfolioDetailsVM PortfolioVM = new PortfolioDetailsVM
{
Projects = await context.Project.Include(x => x.Task).ToListAsync(),
Portfolios = await context.Portfolio.SingleOrDefaultAsync(x => x.PortfolioID == id)
};
return View(PortfolioVM);
}
namespace PEG.Models
{
public partial class PortfolioProject
{
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[Column(TypeName = "datetime2")]
public DateTime? CreatedDate { get; set; }
//RelationsId
[Key]
[Column(Order = 0)]
public int PortfolioId { get; set; }
[Key]
[Column(Order = 1)]
public string ProjectId { get; set; }
//Relations
[ForeignKey("PortfolioId")]
public virtual Portfolio Portfolio { get; set; }
[ForeignKey("ProjectId")]
public virtual Project Project { get; set; }
}
}
答案 0 :(得分:1)
首先,您已经在ViewModels中定义了字段。可以在您的视图中读取它们,但是要使模型绑定起作用,您需要使用get和set访问器将它们声明为属性:
public class PortfolioDetailsVM
{
//...
public Portfolio Portfolio { get; set; }
public PortfolioProject PortfolioProject { get; set; }
public IEnumerable<Project> Projects { get; set; }
}
这应该使您的绑定代码有效。
第二,您使用模型绑定的方式略有不正确。尽量不要直接绑定到数据模型(例如PortfolioProject的类型)。您要绑定的模型不应包含对数据模型类型的任何引用。
相反,我通常只声明我要绑定到的模型中真正需要的内容,这样我就不必再使用ol'Bind
属性。您的情况的一个简单示例:
public class DetailsAddProjectVM
{
public string SelectedProjectId { get; set; }
}
以相应的形式:
@model PortfolioDetailsVM
<select asp-for="SelectedProjectId" class="custom-select form-control">
...
</select>
哪个发布到
public async Task<IActionResult> AddProject(DetailsAddProjectVM bindingModel)
{
//look ma, no [Bind]!
var projectid = bindingModel.SelectedProjectId;
}
当然,要渲染相应的表单,还必须在原始SelectedProjectId
中声明一个PortfolioDetailsVM
属性。
如您所见,您根本没有绑定。