我们正在制作电影评级网络应用程序,但我们遇到错误对象引用未设置为对象的实例。即使我们在导航回评级页面时出现错误,评级也会增加。
位指示:
public ActionResult RateMedia(string Item, int Rate)
{
PopMediaDDL();
var userStore = new UserStore<ApplicationUser>(ldb);
var manager = new UserManager<ApplicationUser>(userStore);
var user = manager.FindById(User.Identity.GetUserId());
MediaRating rate = new MediaRating();
rate.Title = ldb.MediaData.First(c => c.MediaName == Item);
rate.Rating = Rate;
rate.RateFor = user;
ldb.MediaRatingData.Add(rate);
ldb.SaveChanges();
return View();
}
型号:
public class MediaRating
{
[Key]
public int MediaRatingID { get; set; }
public virtual ApplicationUser RateFor { get; set; }
public virtual Media Title { get; set; }
public int Rating { get; set; }
}
查看:
@using CollectionCompanion.Extensions
@using CollectionCompanion.Models;
@model CollectionCompanion.Models.UserCollectionItem
<h2>Rate Media!</h2>
<hr />
@using (Html.BeginForm())
{
<div style="width:80%;">
<div id="AddColItem">
@Html.AntiForgeryToken()
@Html.TextBoxFor(m => m.Item, new { @id = "MediaItem",@style= "color:black;", @name = "MediaItem", @class = "form-control" })
</div>
<div id="AddColDD">
<select id="Rate" name="Rate">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div id="AddColBTN">
<input type="hidden" name="UserCollection" value="@ViewBag.UserCollectionID" />
<button type="submit" class="btn btn-default btn-info"><span class="glyphicon glyphicon-plus"></span> Add</button>
</div>
</div>
}
@foreach (MediaRating rating in ViewBag.Ratings)
{
<div>
<div style="width:50%;">
@rating.Title.MediaName
</div>
<div style="width:50%">
@rating.Rating
</div>
</div>
}
<script type="text/javascript">
$("#MediaItem").autocomplete({
change: function (event, ui) { },
source: "../../Media/GetMediaResult/",
minLength: 2
}
);
</script>
答案 0 :(得分:0)
您将在控制器操作结束时返回没有模型的视图:
f(x) = x
我怀疑您获得了空引用异常,因为您没有提供填充视图所需的模型和ViewBag。它应该看起来类似于HttpGet控制器操作,因为它提供了构建View所需的一切。