简体   繁体   中英

Inserting value to DB

I am new to DNN and .NEt in general so I probably made many mistakes here. My goal is to have a DNN module where you can upload an image with title and description - I did that by properly modifying the template module, using the item model/controller, etc. I tried to add an item rating model/controller, but I can't figure out where exactly I made the mistake.

My goal is for each item that was added to have an "Upvote" button, and that button would simply add an entry to the 2nd table I made in the database to count as an upvote. But with the current code, I can't get to a successful insert to db.

I'm working on an MVC module template and on DNN8 as a test environment.

Here is the project on my github (think it will be much easier): https://github.com/mheonyae/rating

The whole frontend can be seen in 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>

I am totally new to this so any in-depth explanation will be highly appreciated.

Check out my DNN MVC module tutorial example that I use for training. It has a fully working CRUD (Create/Read/Update/Delete) data repository and an image upload.

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

You can download the source package and install the module to test.

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

Leave a comment with any further questions.

A couple of observations with your code that might be causing the issue:

For some reason in your Index razor view you opted to invoke Item.UpvoteRating and then call respective controller instead of calling ItemRating.Upvote , which I think makes more sense (this however is probably not an issue):

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

Another thing (and I suspect this is the issue) is the properties on your ItemRating model (or lack thereof to be precise):

if you check out your database scripts ( DataProviders/.../00.00.01.SqlDataProvider ) you will see ItemRatingId as a PK for your Memeometer_Item_Ratings table:

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

while in your object you've got correct decorators, the class itself didn't have the property:

    [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....
    }

hopefully that fixes the issue for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM