在.net核心剃刀页面中使用远程属性.控制器的动作方法中参数未获取值 [英] Using remote attribute in .net core razor pages. The Parameter does not get value in Action Method of Controller

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

问题描述

我正在使用

I am using Remote Attribute validation The 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核心剃刀页面中使用远程属性.控制器的动作方法中参数未获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆