2017-06-14 86 views
0

我在我的视图中有“firstname”字段。 我的问题是如何手动添加值或使用我的控制器操纵字段。 注:我的观点是已经连接到Index.cshtml文件和我的控制器的名字是FormsControllerSitecore MVC控制器

Firstname: @Html.Sitecore().Field("firstname") 
+0

准许使用必要项目的最佳做法是否将模型传递给您的视图?在将模型传递给您的视图之前,您是否无法掌握字段值! – kandroid

回答

0

最有可能的,你就可以做你需要修改RenderingContext.Current.Rendering.Item什么,但也有其他变体。看看这几个很好的帖子,描述可用选项herehere

0

无论是在您的视图分配@ Html.Sitecore()。菲尔德(“名字”)给一个变量,做视图中的逻辑(不是最好的MVC方法)。

@{ 
var firstName = Html.Sitecore().Field("firstname"); 
firstName = AddedCode + firstName; 
} 
Firstname:@firstName 

或者在你的控制器创建一个部分类来扩展你的模型要传递到与已经操纵姓价值的看法。

0

如果你想操纵Sitecore的,这是做一个这样的项目,虽然你平时不希望编辑从代码Sitecore的项目:

 var sitecoreItem = Sitecore.Context.Item; 
     sitecoreItem = Sitecore.Data.Database.GetDatabase("master").GetItem(sitecoreItem.ID); 
     using (new EditContext(sitecoreItem, SecurityCheck.Disable)) 
     { 
      sitecoreItem["Navigation title"] = "I have just Edited"; 
     } 

请注意,我第一次获得该项目和从主数据库获取项目,因为从Web数据库编辑项目没有意义。

0

当我从您所提供的代码中看到的,我的意思是和平的代码:

@Html.Sitecore().Field("firstname") 

你想操纵你的背景项目的“姓名”字段。 如果是这样,所有你需要的控制器的适当行动中做的是这样的:

using (new Sitecore.SecurityModel.SecurityDisabler()) 
{ 
    Sitecore.Context.Item.Editing.BeginEdit(); 
    try 
    { 
     Sitecore.Context.Item["firstname"] = "set value you want";  
     Sitecore.Context.Item.Editing.EndEdit(); 
    } 
    catch (Exception) 
    {  
     Sitecore.Context.Item.Editing.CancelEdit(); 
    } 
} 

注意:在使用SecurityDisabler()意味着你的情况下用户可能没有上下文项权限。所以这只是一个例子。使用The Security Editor