预填充HTML编辑器Asp.net MVC [英] Prefill Html Editor Asp.net MVC

查看:67
本文介绍了预填充HTML编辑器Asp.net MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在页面加载时填充 @ Html.Editor 的值.这是我失败的尝试:

I want to fill with a value of the @Html.Editor when the page is loaded. 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

这是我的控制器:

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

说值不能为空.

我该如何纠正?谢谢.

推荐答案

我怀疑您正在尝试为模型分配一个可能为null的字符串,该字符串永远不能为null.创建一个ViewModel类,并改用它:

I suspect you are trying to assign a string that could be null to your model, which can never be null. Create a ViewModel class and use it instead:

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

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

然后在您看来:

@model MyViewModel

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

这篇关于预填充HTML编辑器Asp.net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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