[英]Passing model to asp-for | ASP.net Core
I am trying to create a button which takes Model values, calculates them, them re-passes the model (to not lost info already inputted).我正在尝试创建一个按钮,它采用 Model 值,计算它们,它们重新传递 model (不会丢失已经输入的信息)。 I've tried passing the model for asp-for="@Model" but that just fires an error saying that asp-for cannot be null/blank.
我已经尝试将 model 传递给 asp-for="@Model" 但这只会引发一个错误,指出 asp-for 不能为空/空白。
Model Model
{
public int JobId { get; set; }
[Required]
[Display(Name = "Name")]
public string JobName { get; set; }
[Display(Name = "Description")]
public string JobDescription { get; set; }
[Display(Name = "Postcode From")]
public string PostcodeFrom { get; set; }
[Display(Name = "Postcode To")]
public string PostcodeTo { get; set; }
[Display(Name = "Mileage")]
public int TotalMiles { get; set; }
public int? CustomerId { get; set; }
public Customer Customer { get; set; }
}
View Model查看 Model
public class JobDetailViewModel
{
public Job Job { get; set; }
public Customer Customer { get; set; }
public List<SelectListItem> Customers { get; set; }
}
Controller Actions Controller 操作
public IActionResult Create()
{
var jobDetailViewModel = new JobDetailViewModel();
jobDetailViewModel.Customers = _context.Customers.Select(c => new SelectListItem { Value = c.CustomerId.ToString(), Text = c.CustomerName }).ToList();
return View(jobDetailViewModel);
}
and
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult CalculateMilage(JobDetailViewModel model)
{
model.Job = new Job();
model.Job.TotalMiles = 123;
return View("Create", model);
}
Attempted button call尝试按钮调用
@model DHSCRM.ViewModels.JobDetailViewModel
<form asp-action="CalculateMilage" method="post">
<input type="hidden" asp-for="@Model" />
<button class="btn btn-info" value="Calculate">Calculate Milage</button>
</form>
Whenever I hit the button it either passes a blank model, or fails.每当我按下按钮时,它要么通过空白 model,要么失败。 I've tried to ignore asp-for but again it passes in blank, and I'm not entirely sure why.
我试图忽略 asp-for 但它再次以空白传递,我不完全确定为什么。
Can anyone help please?有人可以帮忙吗?
Thanks!谢谢!
EDIT - Entire view编辑 - 整个视图
@model DHSCRM.ViewModels.JobDetailViewModel
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Job</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Job.JobName" class="control-label"></label>
<input asp-for="Job.JobName" class="form-control" />
<span asp-validation-for="Job.JobName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Job.JobDescription" class="control-label"></label>
<input asp-for="Job.JobDescription" class="form-control" />
<span asp-validation-for="Job.JobDescription" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Job.PostcodeFrom" class="control-label"></label>
<input asp-for="Job.PostcodeFrom" class="form-control" />
<span asp-validation-for="Job.PostcodeFrom" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Job.PostcodeTo" class="control-label"></label>
<input asp-for="Job.PostcodeTo" class="form-control" />
<span asp-validation-for="Job.PostcodeTo" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Job.TotalMiles" class="control-label"></label>
<input asp-for="Job.TotalMiles" class="form-control" />
<span asp-validation-for="Job.TotalMiles" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Customer.CustomerName" class="control-label"></label>
<select asp-for="Job.CustomerId" asp-items="@Model.Customers">
<option selected></option>
</select>
<span asp-validation-for="Job.CustomerId" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<form asp-action="CalculateMilage" method="post">
<input type="hidden" asp-for="@Model" />
<button class="btn btn-info" value="Calculate">Calculate Milage</button>
</form>
<hr />
@TempData["ButtonValue"]
<hr />
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
You can use jquery to calculate the fields of your form.您可以使用 jquery 来计算表单的字段。 This is just a sample.
这只是一个示例。
At first give ids to your controls:首先为您的控件提供 ID:
<div class="form-group">
<label asp-for="Job.PostcodeFrom" class="control-label"></label>
<input asp-for="Job.PostcodeFrom" id="postCodeFrom" class="form-control" />
<span asp-validation-for="Job.PostcodeFrom" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Job.PostcodeTo" class="control-label"></label>
<input asp-for="Job.PostcodeTo" id="postCodeTo" class="form-control" />
<span asp-validation-for="Job.PostcodeTo" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Job.TotalMiles" class="control-label"></label>
<input asp-for="Job.TotalMiles" id="totalMiles" class="form-control" />
<span asp-validation-for="Job.TotalMiles" class="text-danger"></span>
</div>
....
replace代替
<form asp-action="CalculateMilage" method="post">
<input type="hidden" asp-for="@Model" />
<button class="btn btn-info" value="Calculate">Calculate Milage</button>
</form>
with和
<button class="btn btn-info" id="btnCalculate"> Calculate Milage</button>
and put the script in the script section:并将脚本放在脚本部分:
$(document).ready(function () {
"use strict";
$(document).on("click", "#btnCalculate", (function (e) {
e.preventDefault();
e.stopImmediatePropagation();
var postCodeFrom = $("#postCodeFrom").val();
var postCodeTo = $("#postCodeTo").val();
var total= postCodeFrom+postCodeTo;
$("#totalMiles").val(total);
}));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.