System.InvalidOperationException: 'Nullable object must have a value.'

Viewed 42

I'm only starting my adventure with programming, and currently im working on a e-commerce type of aplication in mvc. Recently i tried to make it possible to apply more then one photo and since then i have problems with controller that after clicking on the card was redirecting user to the product page. After click the error appears "System.InvalidOperationException: 'Nullable object must have a value.'" and it points to the values that have "null" value in database, but are not required and before it worked well, also it started showing a page with information that controller could not be found. Below im showing my code. Thank you everyone in advance.

View

@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>

Model

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; }
}

}

Controller

 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);
    }

Okay, so i solved my issue, i don't know will it be any help to someone. In the Model and Controller i added the "?" to the int values that were not required.

0 Answers
Related