简体   繁体   English

将 model 传递给 asp-for | ASP.net核心

[英]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.

相关问题 通过 asp-for="..." 将字符串值传递到 ASP.Net 核心中的 Model 无法正常工作 - Passing a String value through asp-for=“…” into a Model in ASP.Net core isn't working the way it should 将值从“选择asp-for”传递到“ textarea” asp.net核心剃须刀 - Passing value from “select asp-for” to a “textarea” asp.net core razor ASP.NET 核心 -&gt; Label asp-for 不返回 label 文本 - ASP.NET Core -> Label asp-for not returning label text 指定“asp-for”HTML标记的格式(ASP.NET Core) - Specify a format for “asp-for” HTML Tag (ASP.NET Core) 复选框中的 asp-for 抛出和 asp.net 核心中的错误 - asp-for in checkbox throws and error in asp.net core ASP.NET Core MVC:asp-for 正在渲染不完整的 id 和 name 属性 - ASP.NET Core MVC: asp-for is rendering incomplete id and name attributes 将数据传递到Asp.Net Core绑定模型中 - Passing data into an Asp.Net Core bound model 传递连接表以查看为 model Asp.Net Core - Passing joined table to view as model Asp.Net Core ASP.NET 核心 - 如何在另一个自定义标签助手中从 asp-for 标签助手获取输入 ID - ASP.NET Core - How to get the input ID from asp-for tag helper in another custom tag helper 当在剃刀页面 ASP.NET Core MVC 中与 asp-for 一起使用时,Textarea 不会在其中显示任何内容 - Textarea won't show anything inside of it when used with asp-for in razor-pages ASP.NET Core MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM