简体   繁体   English

如何使用MVC操作向视图添加HtmlPrefix?

[英]How to add a HtmlPrefix to Views Using MVC Actions?

I have the following action to call the Multiple partial Views in a Single page. 我有以下操作来在单个页面中调用多个局部视图。

public ActionResult AddDetail(string Key)
{
    var viewData = new ViewDataDictionary(ViewData)
            {
                TemplateInfo = new System.Web.Mvc.TemplateInfo
                {
                    HtmlFieldPrefix = string.Format("{0}", key)
                }
            };
    return View(key);
 }

 [View]
 @model Customer
 @Html.TextBoxFor(model=>model.Name)

The above code gives me the html rendered as 上面的代码给了我html呈现为

<input type="text" name="Name"/> 

But, I wants to have 但是,我想要

<input type="text" name="Customer.Name" /> 

If I pass the AddDetail("Customer") action. 如果我通过AddDetail("Customer")动作。

I don't know how to get the ViewContext and append the Prefix to the Views. 我不知道如何获取ViewContext并将Prefix到视图。

Could someone please help me to add a prefix? 有人可以帮我添加前缀吗?

Try this :) 尝试这个 :)

public ActionResult AddDetail(string Key)
{
    ViewData.TemplateInfo.HtmlFieldPrefix = string.Format("{0}", key)
    return View(key);
}

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

相关问题 .Net MVC如何向各个视图添加脚本 - .Net MVC how to add scripts to individual views 我如何在Razor的部分视图中获取HTMLPREFIX - How can I get HTMLPREFIX inside the partial view i Razor 如何在不使用MVC中使用ViewBag的情况下在控制器和视图之间共享数据 - How to share data between controller and views without using ViewBag in MVC 如何将项目添加到列表中<T>从控制器中的表单输入而不使用视图? ASP.NET MVC C# - How can I add items to a list<T> from form input in a controller without using views? ASP.NET MVC C# 动态添加视图ASP.NET MVC - Dynamically Add Views ASP.NET MVC MVC:为所有操作添加输出缓存 - MVC: Add output cache for all actions 如何将某些视图/操作限制为登录用户? - How to restrict some views/actions to logged in users? 使用MVCContrib对MVC 3控制器和视图进行单元测试时,将键和值添加到RouteData - Add Keys and Values to RouteData when using MVCContrib to unit test MVC 3 controllers and Views c# 使用“添加新脚手架项目”的“MVC Controller with Views”代码生成器出错 - c# Error With code Generator For 'MVC Controller with Views' using 'Add New Scaffolded Item' MVC 4 - 如何使用Ajax.ActionLink呈现带有节的视图? - MVC 4 - How to render Views with Sections using Ajax.ActionLink?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM