net核心剃刀页面中使用远程属性

net核心剃刀页面中使用远程属性

本文介绍了在.net核心剃刀页面中使用远程属性.控制器的动作方法中参数未获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用远程属性验证该方法在textchange事件上成功调用.但是,控制器的操作方法中的参数无法获取该字段的值.

I am using Remote Attribute validationThe method is invoked successfully on textchange event. However, the parameters in the action method of the controller does not get the value of the field.

这是HomeController.cs中的操作方法".调用时,名称参数保持为.如果有人解决了这个问题,我将很高兴

Here is the Action Method in the HomeController.cs. When invoked the Name parameter remains null. I will be pleased if someone solve this problem

    [AcceptVerbs("Get", "Post")]
    public async Task<ActionResult> IsExist(string Name)
    {
        List<Keywords> keywords = new List<Keywords>();
        HttpClient client = _api.Initial();
        HttpResponseMessage res = await client.GetAsync("api/Keywords");
        if (res.IsSuccessStatusCode)
        {
            var result = res.Content.ReadAsStringAsync().Result;
            keywords = JsonConvert.DeserializeObject<List<Keywords>>(result);
        }
        if (keywords.FirstOrDefault(x => x.Name == Name) == null)
        {
            return Json(false);
        }
        else
        {
            return Json(true);
        }}

这是模型

 public partial class Keywords
{
    public int Id { get; set; }
    [Display(Name = "Name of Keyword")]
    [Required]
    [Remote(action: "IsExist",controller: "Home", ErrorMessage = "Keyword already present!")]
    public string Name { get; set; }}

这是我要在其中进行验证的剃刀页面

Here is the razor page in which I want to implement validation

    @page
    @model Consumer.Pages.Keyword.CreateModel

    @{
        ViewData["Title"] = "Create";
     }
<h1>Create</h1>

<h4>Keywords</h4>
<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="Keywords.Name" class="control-label"></label>
                <input asp-for="Keywords.Name" class="form-control" />
                <span asp-validation-for="Keywords.Name" class="text-danger"></span>
            </div>

            <div class="form-group">
                <label asp-for="Keywords.Department" class="control-label"></label>
                <select asp-for="Keywords.DepartmentId" class="form-control" asp-items="ViewBag.Department"></select>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-page="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

推荐答案

我找到了解决方案.到

  1. 删除模型类定义中的 partial .
  2. 在剃须刀页面中替换
<input asp-for="Keywords.Name" class="form-control" />

<input asp-for="Keywords.Name" name="Name" class="form-control" />

这篇关于在.net核心剃刀页面中使用远程属性.控制器的动作方法中参数未获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 21:01