簡體   English   中英

將值插入數據庫

[英]Inserting value to DB

我是 DNN 和 .NEt 的新手,所以我可能在這里犯了很多錯誤。 我的目標是擁有一個 DNN 模塊,您可以在其中上傳帶有標題和描述的圖像 - 我通過正確修改模板模塊、使用項目模型/控制器等來做到這一點。我嘗試添加一個項目評級模型/控制器,但是我無法弄清楚我到底在哪里犯了錯誤。

我的目標是為每個添加的項目添加一個“Upvote”按鈕,該按鈕只會向我在數據庫中創建的第二個表添加一個條目,以計為一個 upvote。 但是使用當前的代碼,我無法成功插入數據庫。

我正在研究 MVC 模塊模板,並將 DNN8 作為測試環境。

這是我github上的項目(覺得會容易很多): https : //github.com/mheonyae/rating

整個前端可以在 Index.cshtml 中看到:

<div id="Items-@Dnn.ModuleContext.ModuleId">
    @if (Model.Count() == 0)
    {
        <p>No meme's submited *SadPanda*</p>
    }
    else
    {
        <ul class="tm_tl">
            @foreach (var item in Model)
            {
                <li class="tm_t">
                    <h3>@item.ItemName</h3>
                    <div class="upvote">



                        @Html.ActionLink(
                            "Upvote",                                                  // linkText
                            "UpvoteRating",                                    // actionName
                            "Item",                                                   // controllerName
                            new {                                                     // routeValues
                                item = item
                            },
                            null                                                      // htmlAttributes
                        )


                    </div>
                    <div class="tm_td">
                        <img src="~/desktopmodules/MVC/Memeometer/Memes/@item.ImagePath" style="float:left; width:220px; height:auto;" />

                    </div>
                    <div class="rating">
                        Rating:
                    </div>
                    @{
                        if (Dnn.ModuleContext.IsEditable)
                        {
                            <div>
                                <a href="@Url.Action("Edit", "Item", new {ctl = "Edit", itemId = item.ItemId})">@Dnn.LocalizeString("EditItem")</a>
                                <a href="@Url.Action("Delete", "Item", new {itemId = item.ItemId})">@Dnn.LocalizeString("DeleteItem")</a>
                            </div>
                        }
                    }
                </li>
            }
        </ul>
    }
</div>

我對此完全陌生,因此任何深入的解釋都將受到高度贊賞。

查看我用於訓練的 DNN MVC 模塊教程示例。 它有一個完整的 CRUD(創建/讀取/更新/刪除)數據存儲庫和一個圖像上傳。

https://github.com/DotNetNuclear/DnnRestaurantMenu/tree/master/RestaurantMenu.MVC

可以下載源碼包安裝模塊進行測試。

https://github.com/DotNetNuclear/DnnRestaurantMenu/releases

如有任何其他問題,請發表評論。

對您的代碼的一些觀察可能會導致問題:

出於某種原因,在您的Index razor 視圖中,您選擇調用Item.UpvoteRating然后調用相應的控制器而不是調用ItemRating.Upvote ,我認為這更有意義(但這可能不是問題):

@Html.ActionLink(
   "Upvote",// linkText
   "Upvote",// actionName
   "ItemRating", // controllerName
   new {// routeValues
          id = item.ItemId // this will have to reflect your actual parameter
       },
   null  // htmlAttributes
)

另一件事(我懷疑這是問題所在)是您的ItemRating模型上的屬性(或者准確地說是缺乏):

如果您查看數據庫腳本( DataProviders/.../00.00.01.SqlDataProvider ),您將看到ItemRatingId作為Memeometer_Item_Ratings表的 P​​K:

CREATE TABLE {databaseOwner}{objectQualifier}Memeometer_Item_Ratings
    (
    ItemRatingId int NOT NULL IDENTITY (1, 1),
    ItemIdFk int NOT NULL,
    PcIdentity nvarchar(MAX) NOT NULL,
    ItemRatingPoints int NOT NULL
    )  ON [PRIMARY]
     TEXTIMAGE_ON [PRIMARY]
GO

雖然在你的對象中你有正確的裝飾器,但類本身沒有屬性:

    [TableName("Memeometer_Item_Ratings")]
    [PrimaryKey("ItemRatingId", AutoIncrement = true)] // decoration looks good
    [Cacheable("ItemRatings", CacheItemPriority.Default, 20)]
    [Scope("ModuleId")]
    public class ItemRating
    {
        public int ItemId { get; set; } = -1; // you have this, 
        public int ItemRatingId { get; set; } = -1; // while it likely needs to be this

        .......your other properties....
    }

希望能解決你的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM