gpt4 book ai didi

c# - ASP.NET MVC3 : DisplayTemplates does not show model value (MVC3 Partial Page)

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:54 25 4
gpt4 key购买 nike

为了学习 DisplayTemplates,我创建了一个“String”DisplayTemplate,如下所示。期望在模型的字符串值之后附加单词“Hello”。但它只显示“你好”这个词。我们如何纠正它?

注意:String.cshtml添加在Views\Contact\DisplayTemplates下

public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}

Controller

public class ContactController : Controller
{

// GET: /Contact/Details/5
public ActionResult Details(int id)
{
Contact myContact = new Contact();
myContact.FirstName = "Lijo";
myContact.LastName = "Cheeran";
myContact.Age = 26;

return View(myContact);

}

}

详细 View

@model MyDisplayAndEditorTemplateTEST.Contact

<fieldset>
<legend>Contact</legend>

<div class="display-label" style="font-weight:bold" >FirstName</div>
<div class="display-field">
@Html.DisplayFor(model => model.FirstName)
</div>

<div class="display-label" style="font-weight:bold">LastName</div>
<div class="display-field">
@Html.DisplayFor(model => model.LastName)
</div>

<div class="display-label" style="font-weight:bold">Age</div>
<div class="display-field">
@Html.DisplayFor(model => model.Age)
</div>

字符串显示模板的部分页面(String.cshtml)

<%= Html.Encode(Model) %> Hello!

@达林。对于编辑器模板,我使用了@Html.EditorFor(model => model.FirstName)。它仍然作为标 checkout 现,如下所示。我们如何将其更改为文本框?

详细信息.cshtml

@model MyDisplayAndEditorTemplateTEST.Contact

<fieldset>
<legend>Contact</legend>

<div>FirstName</div>
<div>
@Html.EditorFor(model => model.FirstName)
</div>

<div class="display-label" style="font-weight:bold">LastName</div>
<div class="display-field">
@Html.DisplayFor(model => model.LastName)
</div>

编辑器模板

@Model TEST

显示模板

Hello! @Model

enter image description here

最佳答案

<%= Html.Encode(Model) %> Hello!是 WebForms 语法。确保您没有混淆 2 个 View 引擎。所以将以下内容放入您的 string.cshtml Razor 显示模板:

@Model Hello!

关于c# - ASP.NET MVC3 : DisplayTemplates does not show model value (MVC3 Partial Page),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9357798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com