MVC电商项目添加多图功能后出现Nullable对象值异常求助
问题:MVC电商应用添加多图上传后详情页跳转异常
刚接触编程,正在开发一个基于MVC的电商类应用。近期添加多图上传功能后,点击商品卡片跳转至商品详情页时出现以下问题:
- 弹出异常信息:
System.InvalidOperationException: 'Nullable object must have a value.',该异常指向数据库中允许为null的非必填字段,但这些字段之前正常可用 - 同时出现控制器未找到的提示
视图代码
@model roomie.Models.Adviewmodel @Styles.Render("~/Content/view_ad.css") @Styles.Render("~/Content/layout.css") <meta name="viewport" content="width=device-width" /> <title>ViewAd</title> <div class="add-container"> <div class="add"> <div class="add_title"> <div> <p2> @Model.pro_name </p2> </div> <div> <p3> AD-ID: @Model.pro_id </p3> </div> </div> <div class="pro_image_container"> <div class="pro_image"> @foreach (var image in Model.pro_img_path) { <img src="@Url.Content(image)" alt="Image" /> } </div> </div> <div class="add_description"> <p4> Opis: </p4> <p> @Model.pro_description</p> </div> <div class="add_details"> <table class="add_basic_info"> <tr> <td style="font-weight: bold;"> Cena: </td> <td> @Model.pro_price </td> </tr> <tr> <td style="font-weight: bold;"> Kategoria: </td> <td> @Model.pro_type </td> </tr> <tr> <td style="font-weight: bold;"> Adres: </td> <td> @Model.nazwa_miasta, @Model.pro_address_street @Model.pro_address_building_num, Piętro: @Model.pro_address_floor </td></tr> <tr> <td style="font-weight: bold;"> Kontakt: </td> <td> @Model.user_contact </td> </tr> </table> <div> <p4> Co w mieszkaniu: </p4></div> <div class="bool_info"> <div class="icon"> @if (@Model.pro_TV == true) { <img src="~/Content/img_icons/tv.png" /> } </div> <div class="icon"> @if (@Model.pro_ac == true) { <img src="~/Content/img_icons/ac.png" /> } </div> <div class="icon"> @if (@Model.pro_elevator == true) { <img src="~/Content/img_icons/elevator.png" /> } </div> <div class="icon"> @if (@Model.pro_handicapped == true) { <img src="~/Content/img_icons/hcp.png" /> } </div> <div class="icon"> @if (@Model.pro_internet == true) { <img src="~/Content/img_icons/internet.png" /> } </div> <div class="icon"> @if (@Model.pro_parking == true) { <img src="~/Content/img_icons/parking.png" /> } </div> <div class="icon"> @if (@Model.pro_pet == true) { <img src="~/Content/img_icons/pet.png" /> } </div> <div class="icon"> @if (@Model.pro_smoking == true) { <img src="~/Content/img_icons/smoking.png" /> } </div> </div> </div> </div> </div> <div class="user_data"> @if (Convert.ToInt32(Session["user_id"]) == Convert.ToInt32(Model.pro_fk_user)) { @Html.ActionLink("Close this Ad", "DeleteAd", new { @class = "button", id = Model.pro_id }) } else { <div class="user_data"> <p> Ogłoszenie opublikowane przez: </p> <div class="data"> <div class="user_photo"> @if (String.IsNullOrEmpty(Model.user_image)) { <img src="~/Content/img_icons/user.png" /> } else { <img src="@Url.Content(@Model.user_image)" /> } </div> <div class="user_info"> <table class="info"> <tr> <td> Użytkownik: </td> <td> @Model.user_name </td></tr> <tr> <td> Kontakt: </td> <td> @Model.user_contact </td></tr> <tr> <td> O użytkowniku: </td> <td> @Model.user_description</td></tr> </table> <div class="user_icons"> <div class="icon"> @if (Model.user_smoke == true) { <img src="~/Content/img_icons/smoking.png" /> } </div> <div class="icon"> @if (Model.user_pet == true) { <img src="~/Content/img_icons/pet.png" /> } </div> </div> </div> </div> </div> } </div>
模型代码
namespace roomie.Models { public class Adviewmodel { public int pro_id { get; set; } public string pro_name { get; set; } public string pro_image { get; set; } public string pro_address_street { get; set; } public int pro_address_building_num { get; set; } public int pro_address_floor { get; set; } public int pro_price { get; set; } public string pro_description { get; set; } public string pro_type { get; set; } public int pro_room_size { get; set; } public int pro_room_num { get; set; } public int pro_bathroom_num { get; set; } public int pro_roommates { get; set; } public string pro_room_gender { get; set; } public bool pro_TV { get; set; } public bool pro_internet { get; set; } public bool pro_ac { get; set; } public bool pro_parking { get; set; } public string pro_heating { get; set; } public bool pro_elevator { get; set; } public bool pro_balcony { get; set; } public bool pro_handicapped { get; set; } public bool pro_smoking { get; set; } public bool pro_pet { get; set; } public int pro_prefe_gender { get; set; } public int pro_fk_user { get; set; } public int user_id { get; set; } public string user_name { get; set; } public string user_image { get; set; } public string user_contact { get; set; } public string user_description { get; set; } public bool user_smoke { get; set; } public bool user_pet { get; set; } public int pro_fk_address_city { get; set; } public string nazwa_miasta { get; set; } public string nazwa_województwa { get; set; } public IEnumerable<string> pro_img_path { get; set; } public IEnumerable<string> pro_img_fk_pro_id { get; set; } } }
控制器代码
public ActionResult ViewAd(int id) { Adviewmodel ad = new Adviewmodel(); tbl_product p = db.tbl_product.Where(x => x.pro_id == id).SingleOrDefault(); ad.pro_id = p.pro_id; ad.pro_name = p.pro_name; //ad.pro_fk_images = p.pro_fk_images; //ad.pro_fk_address_city = p.pro_fk_address_city; ad.pro_address_street = p.pro_address_street; ad.pro_address_building_num = (int)p.pro_address_building_num; ad.pro_address_floor = (int)p.pro_address_floor; ad.pro_price = (int)p.pro_price; ad.pro_description = p.pro_description; ad.pro_type = p.pro_type; ad.pro_room_size = (int)p.pro_room_size; ad.pro_room_num = (int)p.pro_room_num; ad.pro_roommates = (int)p.pro_roommates; ad.pro_TV = (bool)p.pro_TV; ad.pro_internet = (bool)p.pro_internet; ad.pro_ac = (bool)p.pro_ac; ad.pro_parking = (bool)p.pro_parking; ad.pro_heating = p.pro_heating; ad.pro_elevator = (bool)p.pro_elevator; ad.pro_balcony = (bool)p.pro_balcony; ad.pro_handicapped = (bool)p.pro_handicapped; ad.pro_smoking = (bool)p.pro_smoking; ad.pro_pet = (bool)p.pro_pet; //ad.pro_prefe_gender = (int)p.pro_prefe_gender; tbl_user u = db.tbl_user.Where(x => x.user_id == p.pro_fk_user).SingleOrDefault(); ad.user_name = u.user_name; ad.user_image = u.user_image; ad.user_contact = u.user_contact; ad.pro_fk_user = u.user_id; tbl_wojewodztwa_miasta wm = db.tbl_wojewodztwa_miasta.Where(x => x.id == p.pro_fk_address_city).SingleOrDefault(); ad.nazwa_miasta = wm.nazwa_miasta; ad.nazwa_województwa = wm.nazwa_województwa; ad.pro_img_path = Directory.EnumerateFiles(Server.MapPath("~/ Content / user_upload")).Select(x => "~/ Content / user_upload" + Path.GetFileName(x)); return View(ad); }
解决方案
已解决该问题,方法是在Model和Controller中给非必填的int类型字段添加?标记,使其支持可空类型,避免在数据库字段为null时强制转换引发异常。
内容的提问来源于stack exchange,提问作者KonradW
相关产品推荐
相关产品推荐

