我正在使用Create
项目中的ASP.NET MVC
视图更新数据。当我从Create
方法的GET
视图中显示数据时,我发现我从SoftwareLicenseId
的数据库中获取了正确的Model
。但是,当我更正其他字段并保存数据时,我看到相同的Model
,现在使用POST
方法中的更正数据显示SoftwareLicenseId
为null
。我该如何纠正?
// GET
public IActionResult Create(int? Id)
{
// Creating/Entering new data
if(Id == null)
{
return View();
}
// Updating existing data. Control coming when edit button pressed
else
{
var softwareLicensesDetails = _context.SoftwareLicenses.Where(sl => sl.SoftwareLicenseId == Id).SingleOrDefault();
return View(softwareLicensesDetails);
}
}
// ~/SoftwareLicenses/Create
[HttpPost]
public IActionResult Create(SoftwareLicenses softwareLicenses)
{
try
{
//SoftwareLicenseId not present -> New Data. Therefore inserting.
if (softwareLicenses.SoftwareLicenseId <= 0)
{
if (ModelState.IsValid)
{
_context.Add(softwareLicenses);
_context.SaveChanges();
return RedirectToAction("Details");
}
}
// SoftwareLicenseId already there in database -> Data present in database. Therefore, updating.
else
{
if (ModelState.IsValid)
{
softwareLicenses.SoftwareLicenseId = Id;
_context.Update(softwareLicenses);
_context.SaveChanges();
return RedirectToAction("Details");
}
}
}
catch(Exception ex)
{
ViewBag.ErrorMessage = ex.Message.ToString();
}
var errors = ModelState.Values.SelectMany(v => v.Errors);
return View(NotFound());
}
修改
查看:
<div class="card-body card-padding" id="softwareLicensesCreate">
<form method="post" asp-action="Create" asp-controller="SoftwareLicenses">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="row">
<div class="col-sm-6 m-t-20">
<div class="input-group fg-float">
<div class="fg-line">
<label asp-for="ProductName" class="fg-label" for="productName">Product Name</label>
<input asp-for="ProductName" class="form-control" id="productName" name="productName" required />
<span asp-validation-for="ProductName" class="text-danger"></span>
</div>
</div>
</div>
<div class="col-sm-6">
<p class="c-black f-500 no-margin">Purchase Date</p>
<div class="input-group fg-float">
<span class="input-group-addon"><i class="zmdi zmdi-calendar"></i></span>
<div class="dtp-container">
<input type="text" asp-for="PurchaseDate" class="form-control date-picker" placeholder="Click here..." id="purchaseDate" name="purchaseDate" required data-date-form />
<span asp-validation-for="PurchaseDate" class="text-danger"></span>
</div>
</div>
</div>
</div>
<div class="row m-t-20">
<div class="col-sm-6 m-t-20">
<div class="input-group fg-float">
<div class="fg-line">
<input asp-for="Cost" class="form-control" id="cost" name="cost" required />
<label asp-for="Cost" class="fg-label" for="cost">Cost</label>
<span asp-validation-for="Cost" class="text-danger"></span>
</div>
</div>
</div>
<div class="col-sm-6 m-t-20">
<div class="fg-line form-chose">
<label class="fg-label" for="subscriptionType">Subscription Type: </label>
<select id="subscriptionType" asp-for="SubscriptionType" data-placeholder="Select Subscription Type" class="chosen disabledropdowncntrl" name="subscriptionType" required>
<option value="" disabled selected>Select Subscription Type</option>
<option value="Monthly">Monthly</option>
<option value="Yearly">Yearly</option>
<option value="OneTime">One-Time</option>
</select>
</div>
</div>
</div>
<div class="row m-t-20">
<div class="col-sm-6 m-t-20">
<div class="input-group fg-float">
<div class="fg-line">
<input asp-for="RenewalCost" class="form-control" id="renewalCost" name="renewalCost" />
<label asp-for="RenewalCost" class="fg-label" for="renewalCost">Renewal Cost</label>
<span asp-validation-for="RenewalCost" class="text-danger"></span>
</div>
</div>
</div>
<div class="col-sm-6 m-t-20">
<div class="input-group fg-float">
<div class="fg-line">
<input asp-for="LicenseKey" class="form-control" id="licenseKey" name="licenseKey" />
<label asp-for="LicenseKey" class="fg-label" for="licenseKey">License Key</label>
<span asp-validation-for="LicenseKey" class="text-danger"></span>
</div>
</div>
</div>
</div>
<div class="row m-t-20">
<div class="col-sm-6 m-t-20">
<div class="input-group fg-float">
<div class="fg-line">
<input asp-for="URL" class="form-control" id="url" name="url" />
<label asp-for="URL" class="fg-label" for="url">URL</label>
<span asp-validation-for="URL" class="text-danger"></span>
</div>
</div>
</div>
<div class="col-sm-6 m-t-20">
<div class="input-group fg-float">
<div class="fg-line">
<input asp-for="Username" class="form-control" id="username" name="username" />
<label asp-for="Username" class="fg-label" for="username">Username</label>
<span asp-validation-for="Username" class="text-danger"></span>
</div>
</div>
</div>
</div>
<div class="row m-t-20">
<div class="col-sm-6 m-t-20">
<div class="input-group fg-float">
<div class="fg-line">
<input asp-for="Password" class="form-control" id="password" name="password" />
<label asp-for="Password" class="fg-label" for="password">Password</label>
<span asp-validation-for="Password" class="text-danger"></span>
</div>
</div>
</div>
<div class="col-sm-6 m-t-20">
<div class="toggle-switch toggle-switch-demo">
<label for="InUse" class="ts-label">In Use?</label>
<input id="inUseRadio" type="checkbox" class="disablecntrl" asp-for="InUse" checked/>
<label for="InUse" class="checkInUse ts-helper"></label>
</div>
</div>
</div>
<div class="row m-t-20">
<div class="col-sm-6">
<p class="c-black f-500 no-margin">Closing Date</p>
<div class="input-group fg-float">
<span class="input-group-addon"><i class="zmdi zmdi-calendar"></i></span>
<div class="dtp-container">
<input type="text" asp-for="ClosingDate" class="form-control date-picker" placeholder="Click here..." id="closingDate" name="closingDate" data-date-form />
</div>
</div>
</div>
<div class="col-sm-6 m-t-20">
<div class="input-group fg-float">
<div class="fg-line">
<input asp-for="Users" class="form-control" id="users" name="users" />
<label asp-for="Users" class="fg-label" for="users">Users</label>
<span asp-validation-for="Users" class="text-danger"></span>
</div>
</div>
</div>
</div>
<div class="row m-t-30">
<div class="form-group">
<div class="col-md-12 text-center">
<button type="submit" value="Save" class="btn bgm-orange waves-effect mybtn">SAVE</button>
</div>
</div>
</div>
</form>
</div>
答案 0 :(得分:0)
使用数据绑定将表单直接传递到控制器的模型时,需要为模型的每个元素输入一个输入。尝试在视图上为软件许可标识创建隐藏输入。
您的GET方法正在运行,因为actionlink或任何调用者明确地将id值作为参数传递给您。然后,您通过检索具有该id的对象并将其传递给视图来构建模型。
你目前不是&#34;存储&#34;视图中任何位置的软件许可证ID,因此无法通过POST调用将其传递回控制器。
答案 1 :(得分:0)
这是因为您没有持有您获得的变量值。只需将此行保留在表单标记内,您的问题就会得到解决。这将保留变量的值,您将在post方法中获得值。
<input type="hidden" asp-for="SoftwareLicenseId"/>