1

こんにちは、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>
4

1 に答える 1

6

あなたのアプローチは間違っています。UmbracoがAPIで提供するメソッドを使用する必要があり、データベースに直接データを書き込もうとしないでください。

次のコードを試して、Razorコードから新しいドキュメントを作成してください。

@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); 
}

上記の例はUmbraco4.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);
}
于 2013-03-14T12:10:02.820 に答える