問題:各位程式員,您好,我是 ASP.NET 的新手,并試圖弄清楚為什么會發生這種情況幾個小時,但找不到原因。控制器中的模型在發布后沒有獲得任何屬性。它們都是空值,為什么會這樣?
風景
@model AddProductViewModel
@{
ViewBag.Title = "Add a new Product";
}
<h2 class="text-center">@ViewBag.Title</h2>
<div class="row">
<div class="col-sm-12 offset-lg-2 col-lg-8 offset-xl-3 col-xl-6">
<form asp-action="Add" method="post">
<div class="mb-3">
<label asp-for="@Model.Model" class="form-label">Model Name</label>
<input asp-for="@Model.Model" class="form-control" aria-required="true" />
<span asp-validation-for="Model" class="text-danger"></span>
</div>
<div class="mb-3">
<label asp-for="@Model.Description" class="form-label">Description</label>
<textarea asp-for="@Model.Description" class="form-control" aria-required="true" rows="5"></textarea>
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="mb-3">
<label asp-for="@Model.Colour" class="form-label">Colour</label>
<input asp-for="@Model.Colour" class="form-control" value="" />
<span asp-validation-for="Colour" class="text-danger"></span>
</div>
<div class="mb-3">
<label asp-for="@Model.Size" class="form-label">Size</label>
<input asp-for="@Model.Size" class="form-control" value="" />
<span asp-validation-for="Size" class="text-danger"></span>
</div>
<div class="mb-3">
<label asp-for="@Model.Price" class="form-label">Price</label>
<input asp-for="@Model.Price" class="form-control" value="" />
<span asp-validation-for="Price" class="text-danger"></span>
</div>
<div class="mb-3">
<label asp-for="@Model.ImageData" class="form-label">Image URL</label>
<input asp-for="@Model.ImageData" class="form-control" aria-required="true" />
<span asp-validation-for="ImageData" class="text-danger"></span>
</div>
<div class="mb-3">
<label asp-for="@Model.CategoryId" class="form-label">Category</label>
<select asp-for="@Model.CategoryId" class="form-control">
@foreach (var category in Model.Categories)
{
<option value="@category.Id">@category.Name</option>
}
</select>
<span asp-validation-for="CategoryId" class="text-danger"></span>
</div>
<div class="mb-3">
<label asp-for="@Model.BrandId" class="form-label">Brand</label>
<select asp-for="@Model.BrandId" class="form-control">
@foreach (var brand in Model.Brands)
{
<option value="@brand.Id">@brand.Name</option>
}
</select>
<span asp-validation-for="BrandId" class="text-danger"></span>
</div>
<div class="mb-3">
<input class="btn btn-primary" type="submit" value="Add" />
</div>
</form>
</div>
</div>
@section Scripts {
<partial name="_ValidationScriptsPartial" />
}
控制器
...
[HttpPost]
public async Task<IActionResult> Add(AddProductViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
try
{
await productService.AddProductAsync(model);
return RedirectToAction("Index", "Home");
}
catch (Exception)
{
ModelState.AddModelError("", "Something went wrong");
return View(model);
}
}
...
這是我第一次遇到這樣的事情。我做了另一個非常相似的專案,如果不一樣的話。任何幫助將不勝感激!
uj5u.com熱心網友回復:
您的模型包含命名的屬性Model,這將與您的引數沖突AddProductViewModel model。
因此,只需更改您的引數名稱,如下所示:
[HttpPost]
public async Task<IActionResult> Add(AddProductViewModel addProductViewModel)
{
if (!ModelState.IsValid)
{
return View(addProductViewModel);
}
try
{
await productService.AddProductAsync(addProductViewModel);
return RedirectToAction("Index", "Home");
}
catch (Exception)
{
ModelState.AddModelError("", "Something went wrong");
return View(addProductViewModel);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/532852.html
上一篇:在IIS中使用ASP.NETcoremvc下載發布檔案并復制到客戶端的內部服務器上
下一篇:當字串小或大時添加或洗掉字符
