简体   繁体   English

预填充HTML编辑器Asp.net MVC

[英]Prefill Html Editor Asp.net MVC

I want to fill with a value of the @Html.Editor when the page is loaded. 我想在页面加载时填充@Html.Editor的值。 Here is my failed attempt : 这是我失败的尝试:

 @Html.EditorFor(m => m, new { htmlAttributes = new { @class = "form-control" } })

At the top of the Razor page , I have this : 在“剃刀”页面的顶部,我有以下内容:

@model string

And this is my controller : 这是我的控制器:

public ActionResult YeniBelge(string KimlikNo)
{
    return View((object)KimlikNo);
}

It says value can't be null. 它说值不能为空。

How can I make this correct? 我该如何纠正? Thanks. 谢谢。

用控制器中的数据填充模型,然后使用

@Html.EditorFor(m=>m);

Like @vortex pointed out you need to use the EditorFor template. 就像@vortex指出的那样,您需要使用EditorFor模板。

@Html.EditorFor(model => model.TCKimlikNo, new { htmlAttributes = new { @class = "form-control"  } })

If you have your model loaded at the top of the page this will fill in the form with the current value of the model field. 如果您在页面顶部加载了模型,则将使用模型字段的当前值填写表格。

I suspect you are trying to assign a string that could be null to your model, which can never be null. 我怀疑您正在尝试为模型分配一个可能为null的字符串,该字符串永远不能为null。 Create a ViewModel class and use it instead: 创建一个ViewModel类,并改用它:

public class MyViewModel {
    public string TCKimlikNo { get; set; }
}

public ActionResult YeniBelge(string KimlikNo)
{
    return View(new MyViewModel { KimlikNo = KimlikNo ?? "My Default Value" });
}

And then in your view: 然后您认为:

@model MyViewModel

@Html.EditorFor(m => m.TCKimlikNo, new { htmlAttributes = new { @class = "form-control"  } })

In your controller change to the following. 在您的控制器中更改为以下内容。

  public class KimlikNoViewmodel
    {
        public string KimlikNo { get; set; }
    }

    public ActionResult YeniBelge (string KimlikNo)
    {
        KimlikNoViewmodel viewModel = new KimlikNoViewmodel();
        viewModel.KimlikNo = KimlikNo;

        return View(viewModel);
    }

in your view I suspect that you are not finding the model. 在您看来,我怀疑您没有找到模型。 You need to include your project name when calling the Model. 调用模型时,需要包括您的项目名称。

@model YourProjectName.Controllers.KimlikNoViewmodel

I would suggest actually saving the View model we created within the Models folder under a View models file and then you would call the following instead. 我建议实际上将我们在“模型”文件夹中创建的“视图”模型保存在“视图模型”文件下,然后改为调用以下内容。

@model YourProjectName.Models.KimlikNoViewmodel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM