[英]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
表的 PK:
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.