我目前正在忙于一个使用ASP.NET MVC的项目。 当我尝试将数据发布到模型和控制器时,我在控制器中使用的方法的参数中返回NULL。 控制器:
[HttpPost]
public ActionResult Create(Company company)
{
if (ModelState.IsValid)
{
company.Visible = 1;
company.Created = DateTime.Now;
company.Updated = DateTime.Now;
//Save company to database
cm.AddCompany(company);
cm.SaveChanges();
//Redirect user to Index after created a company
return RedirectToAction(nameof(Index));
}
else
{
return View();
}
}
我的数据库第一个模型:
public Company()
{
this.Purchases = new HashSet<Purchase>();
}
public int ID { get; set; }
public string Name { get; set; }
public string Street { get; set; }
public string HouseNumber { get; set; }
public string Zip { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public string Branche { get; set; }
public string KVKnumber { get; set; }
public string BTWnumber { get; set; }
public System.DateTime Created { get; set; }
public Nullable<System.DateTime> Updated { get; set; }
public Nullable<int> Visible { get; set; }
public virtual ICollection<Purchase> Purchases { get; set; }
我必须将数据发送到模型和控制器的视图:
@model ExamenProject.Database.Company
<form asp-controller="Company" asp-action="Create" method="post" enctype="multipart/form-data">
<div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
<div class="row">
<div class="col-md-12">
<fieldset>
<label asp-for="Name">Naam</label>
<div class="field">
<input asp-for="Name" class="form-control" style="margin-bottom:2%;" type="text"/>
<span asp-validation-for="Name" />
</div>
</fieldset>
</div>
我真的不知道为什么我的Create方法中的参数公司总是返回NULL。