在联系人创建中,我想从下拉列表中分配客户或供应商

In the contact creation I want to assign a customer or supplier from a dropdown

我有两个问题。

1.问题: 当我将我的 selection 添加到剃刀页面时,模型中的所有必填字段验证不再起作用。

没有 selection 的模型验证

添加 selection 时未验证

当下拉菜单位于 Razor 中时 html 验证无效,它会因 NULL 值而崩溃,我想防止这种情况发生。就像没有 select 选项一样。 这是为什么?


2。问题: 当我 select 我的下拉列表中的一位客户 (CustomerDropDown.CusId) 我想将 ID 填写到我的 ID 联系人字段中( contacts.CustomerId)。如何从下拉菜单中获取 selected 值并将其 post 发送给我的联系人 contacts.CustomerId

== c#代码==

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc.Rendering;
using WorkCollaboration.Data;
using WorkCollaboration.Models;

namespace WorkCollaboration.Pages.Contacts
{ 
    public class CreateModel : PageModel
    {
        private readonly WorkCollaboration.Data.WorkCollaborationContext _context;

        public CreateModel(WorkCollaboration.Data.WorkCollaborationContext context)
        {
            _context = context;
        }

    public async Task<IActionResult> OnGetAsync()
        {
            CustomerDropDownDisp = await _context.CustomerDropDown.ToListAsync();  // Added for DropDown
            SupplierDropDownDisp = await _context.SupplierDropDown.ToListAsync();  // Added for DropDown
            return Page();
        }

        [BindProperty]
        public Contact Contact { get; set; }
        public IEnumerable<CustomerDropDown> CustomerDropDownDisp { get; set; }
        public IEnumerable<SupplierDropDown> SupplierDropDownDisp { get; set; }

        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            _context.Contact.Add(Contact);
            await _context.SaveChangesAsync();

            return RedirectToPage("/ContactsOverview/Index");
        }
    }
}

== Razor 页面代码 ==

@page 
@using WorkCollaboration.Models
@model WorkCollaboration.Pages.Contacts.CreateModel

@{
    ViewData["Title"] = "Create";
    ViewData["RandomId"] = Guid.NewGuid().GetHashCode();
}

<h1>Create</h1>
<h4>Contact</h4>
<p>
    <a asp-page="/ContactsOverview/Index">Back to Index</a>
</p>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Contact.ContactId" class="control-label"></label>
                <input asp-for="Contact.ContactId" value='@ViewData["RandomId"]' readonly="readonly" class="form-control" />
                <span asp-validation-for="Contact.ContactId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.LastName" class="control-label"></label>
                <input asp-for="Contact.LastName" class="form-control" />
                <span asp-validation-for="Contact.LastName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.FirstName" class="control-label"></label>
                <input asp-for="Contact.FirstName" class="form-control" />
                <span asp-validation-for="Contact.FirstName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.CustomerId" class="control-label"></label>
            </div>
            <select id="CusId" asp-for="CustomerDropDownDisp" asp-items="@(new SelectList(Model.CustomerDropDownDisp,"CusId","CusName"))">
                <option value="">--Choose Customer--</option>
                <option value="1" selected>None</option>>
            </select>
            <div class="form-group">
                <input asp-for="Contact.CustomerId" class="form-control" />
                <span asp-validation-for="Contact.CustomerId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.SupplierId" class="control-label"></label>
            </div>
            <select id="SupId" asp-for="SupplierDropDownDisp" asp-items="@(new SelectList(Model.SupplierDropDownDisp,"SupId","SupName"))">
                <option value="">--Choose Supplier--</option>
                <option value="1" selected>None</option>>
            </select>
            <div class="form-group">
                <input asp-for="Contact.SupplierId" class="form-control" />
                <span asp-validation-for="Contact.SupplierId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.PrivateStreet" class="control-label"></label>
                <input asp-for="Contact.PrivateStreet" class="form-control" />
                <span asp-validation-for="Contact.PrivateStreet" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.PrivateStreetNo" class="control-label"></label>
                <input asp-for="Contact.PrivateStreetNo" class="form-control" />
                <span asp-validation-for="Contact.PrivateStreetNo" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.PrivateStreetAdditionalInfo" class="control-label"></label>
                <input asp-for="Contact.PrivateStreetAdditionalInfo" class="form-control" />
                <span asp-validation-for="Contact.PrivateStreetAdditionalInfo" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.PrivateZip" class="control-label"></label>
                <input asp-for="Contact.PrivateZip" class="form-control" />
                <span asp-validation-for="Contact.PrivateZip" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.PrivateTown" class="control-label"></label>
                <input asp-for="Contact.PrivateTown" class="form-control" />
                <span asp-validation-for="Contact.PrivateTown" class="text-danger"></span>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Contact.PrivateCountry, htmlAttributes: new { @class = "form-group" })
                <div class="form-group">
                    @Html.DropDownListFor(model => model.Contact.PrivateCountry, new List<SelectListItem>
                    {
                        new SelectListItem {Text = "CH", Value = "CH", Selected = true },
                        new SelectListItem {Text = "D", Value = "D" },
                        new SelectListItem {Text = "FL", Value = "FL" },
                    }, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Contact.PrivateCountry, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                <label asp-for="Contact.PrivatePhone" class="control-label"></label>
                <input asp-for="Contact.PrivatePhone" class="form-control" />
                <span asp-validation-for="Contact.PrivatePhone" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.BusinessPhone" class="control-label"></label>
                <input asp-for="Contact.BusinessPhone" class="form-control" />
                <span asp-validation-for="Contact.BusinessPhone" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.MobilePhone" class="control-label"></label>
                <input asp-for="Contact.MobilePhone" class="form-control" />
                <span asp-validation-for="Contact.MobilePhone" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.Mail" class="control-label"></label>
                <input asp-for="Contact.Mail" class="form-control" />
                <span asp-validation-for="Contact.Mail" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
                <a href="/ContactsOverview/Index" class="btn btn-primary">Back to List</a>
            </div>
        </form>
    </div>
</div>

感谢您就如何获取 selected 值(下拉)到相应的联系人 ID 字段提供的帮助。

这是一个演示:

<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Contact.ContactId" class="control-label"></label>
                <input asp-for="Contact.ContactId" value='@ViewData["RandomId"]' readonly="readonly" class="form-control" />
                <span asp-validation-for="Contact.ContactId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.LastName" class="control-label"></label>
                <input asp-for="Contact.LastName" class="form-control" />
                <span asp-validation-for="Contact.LastName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.FirstName" class="control-label"></label>
                <input asp-for="Contact.FirstName" class="form-control" />
                <span asp-validation-for="Contact.FirstName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.CustomerId" class="control-label"></label>
            </div>
            <select id="CusId" asp-for="CustomerDropDownDisp" asp-items="@(new SelectList(Model.CustomerDropDownDisp,"CusId","CusName"))">
                <option value="">--Choose Customer--</option>
                <option value="1" selected>None</option>>
            </select>
            <div class="form-group">
                <input asp-for="Contact.CustomerId" class="form-control" />
                <span asp-validation-for="Contact.CustomerId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.SupplierId" class="control-label"></label>
            </div>
            <select id="SupId" asp-for="SupplierDropDownDisp" asp-items="@(new SelectList(Model.SupplierDropDownDisp,"SupId","SupName"))">
                <option value="">--Choose Supplier--</option>
                <option value="1" selected>None</option>>
            </select>
            <div class="form-group">
                <input asp-for="Contact.SupplierId" class="form-control" />
                <span asp-validation-for="Contact.SupplierId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.PrivateStreet" class="control-label"></label>
                <input asp-for="Contact.PrivateStreet" class="form-control" />
                <span asp-validation-for="Contact.PrivateStreet" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.PrivateStreetNo" class="control-label"></label>
                <input asp-for="Contact.PrivateStreetNo" class="form-control" />
                <span asp-validation-for="Contact.PrivateStreetNo" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.PrivateStreetAdditionalInfo" class="control-label"></label>
                <input asp-for="Contact.PrivateStreetAdditionalInfo" class="form-control" />
                <span asp-validation-for="Contact.PrivateStreetAdditionalInfo" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.PrivateZip" class="control-label"></label>
                <input asp-for="Contact.PrivateZip" class="form-control" />
                <span asp-validation-for="Contact.PrivateZip" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.PrivateTown" class="control-label"></label>
                <input asp-for="Contact.PrivateTown" class="form-control" />
                <span asp-validation-for="Contact.PrivateTown" class="text-danger"></span>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Contact.PrivateCountry, htmlAttributes: new { @class = "form-group" })
                <div class="form-group">
                    @Html.DropDownListFor(model => model.Contact.PrivateCountry, new List<SelectListItem>
                    {
                        new SelectListItem {Text = "CH", Value = "CH", Selected = true },
                        new SelectListItem {Text = "D", Value = "D" },
                        new SelectListItem {Text = "FL", Value = "FL" },
                    }, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Contact.PrivateCountry, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                <label asp-for="Contact.PrivatePhone" class="control-label"></label>
                <input asp-for="Contact.PrivatePhone" class="form-control" />
                <span asp-validation-for="Contact.PrivatePhone" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.BusinessPhone" class="control-label"></label>
                <input asp-for="Contact.BusinessPhone" class="form-control" />
                <span asp-validation-for="Contact.BusinessPhone" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.MobilePhone" class="control-label"></label>
                <input asp-for="Contact.MobilePhone" class="form-control" />
                <span asp-validation-for="Contact.MobilePhone" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Contact.Mail" class="control-label"></label>
                <input asp-for="Contact.Mail" class="form-control" />
                <span asp-validation-for="Contact.Mail" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
                <a href="/ContactsOverview/Index" class="btn btn-primary">Back to List</a>
            </div>
        </form>
    </div>
</div>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<script>
    $("#CusId").on("change", function () {
        $("#Contact_CustomerId").val($("#SupId").val());
    });
    $("#SupId").on("change", function () {
        $("#Contact_SupplierId").val($("#SupId").val());
    });
</script>

结果: