2013-03-14 27 views
1

你好我想保存umbraco表格的值爲這個我已經使腳本文件,並在這個腳本文件我已經創建了函數來保存數據,並在同一腳本文件中調用此函數和這個腳本文件在宏中使用,我已經在我的頁面的模板中調用這個宏,但它不工作,這種方法是適當的,或者我不得不別的東西我的基本目標是保存數據庫中的數據,而不創建我的用戶控制通過腳本文件在umbraco中保存表格值

代碼

@functions 
{ 
    public void AddToCart() 
    { 
     string con = System.Configuration.ConfigurationManager.AppSettings["umbracoDbDSN"].ToString(); 
     SqlConnection OnCon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["umbracoDbDSN"].ToString()); 
     ItemsDataContext db = new ItemsDataContext(con); 
     var request = HttpContext.Current.Request; 
     string itemcode= request.Form["ItemCode"].ToString(); 
     string itemname = request.Form["ItemName"].ToString(); 
     string itemcategory = Request.Form["ItemCategory"].ToString(); 
     string userid = "Pallavi"; 
     db.sp_AddItems(userid, itemcode, itemcategory, itemname, 0); 


     HttpContext.Current.Session["UserId"] = "Pallavi"; 
    } 
} 


@if (!IsPost) 
{ 
    AddToCart(); 
} 

,並呼籲該宏模板

<umbraco:Macro Alias="Uc_Cart" runat="server"></umbraco:Macro> 

回答

6

你的做法是錯誤的。您必須使用Umbraco在其API中提供的方法,並且不要直接將數據寫入數據庫。

試試這個代碼來創建從剃刀代碼的新的文檔:

@using umbraco.BusinessLogic; 
@using umbraco.cms.businesslogic.web; 
@{ 
    DocumentType dt = DocumentType.GetByAlias("Textpage"); 
    User author = umbraco.BusinessLogic.User.GetUser(0); 
    Document doc = Document.MakeNew("My new document", dt, author, parentID); 
} 

上面的例子是用於一把umbraco 4.x的如果您使用的是Umbraco v6.x,則還可以使用新的API方法:

@{ 
    // get an instance of the contentService 
    var contentService = ApplicationContext.Services.ContentService; 
    // create new content, the last param is the userId and is optional [default = 0] 
    IContent newContent = contentService.CreateContent("My new document", parentID, "Textpage", 0); 
    // set property values 
    newContent.SetValue("propertyAlias", "Value"); 
    // save (or save and publish) 
    contentService.Save(newContent); 
} 
+0

MakeNew方法中的最後一個參數是Document的parentID。它被保存爲這個父項的一個childItem。如果要將文檔保存在根目錄中,則在示例中使用-1作爲parentID – 2013-03-14 13:55:33

+0

,通過使用hello User.GetUser(id)方法中的authorID獲取作者。如果您使用0,您將獲得Admin用戶,或者您可以使用任何其他現有用戶的ID。 – 2013-03-14 19:03:01

+0

即時通訊沒有得到我的應用程序getuser方法和什麼概率發現是我的表單不張貼所以我能做什麼 – iProgrammer 2013-03-15 06:22:09

相關問題