Question

I am new with MVC and I am trying to edit a tutorial I completed to understand how everything works. Originally, the tutorial had a search function that let you search by two string parameters, one that check the movie title and one that checks the movie genre, returning the query of any matching values with those parameters.

I am trying to make an actors search which searches by actor name and actor age (an int). I keep getting non-nullable type issues with age, and I have tried everything from declaring age as int?, to .ToString()'ing the IsNullOrEmpty, but nothing has worked.

Can someone show me how to go about this?

Below is the tutorial's searchIndex function. (two strings)

public ActionResult SearchIndex(string movieGenre, string searchString)
    {
        var GenreList = new List<String>();
        var GenreQry = from d in db.Movies orderby d.Genre select d.Genre;

        GenreList.AddRange(GenreQry.Distinct());
        ViewBag.movieGenre = new SelectList(GenreList);


        var movies = from m in db.Movies select m;

        if (!String.IsNullOrEmpty(searchString))
        {
            movies = movies.Where(s => s.Title.Contains(searchString));
        }

        if (string.IsNullOrEmpty(movieGenre))
        {
            return View(movies);
        }
        else
        {

            return View(movies.Where(x => x.Genre == movieGenre));
        }

    }

below is my version (int Age and a string)

public ActionResult SearchIndex(int ageValue, string searchString)
        {
            var AgeList = new List<int>();
            var AgeListQry = from d in db.Actors orderby d.Age select d.Age;

            AgeList.AddRange(AgeListQry.Distinct());
            ViewBag.ageValue = new SelectList(AgeList);

            var actors = from a in db.Actors select a;

            if (!String.IsNullOrEmpty(searchString))
            {
                actors = actors.Where(s => s.Name.Contains(searchString));

            }

            if ("Trying to see if Age is null - this fails on page load")
            {
                return View(actors);
            }
            else
            {
                return View(actors.Where(x => x.Age == ageValue));
            }
        }

My initialization of int Age in the Model is:

public DateTime BirthDate { get; set; }//user input
public int Age
            {
                get { 
                    return (int)(DateTime.Now - BirthDate).TotalDays / 365; 
                }

My View of Age (not sure how to fully implement an int here

<p>
    @Html.ActionLink("Create New", "Create")
    @using (Html.BeginForm("SearchIndex","Movies",FormMethod.Get)){  
        <p>Age: @Html.TextBox("ageValue") 
         <p> Title: @Html.TextBox("SearchString")<br />  
         <input type="submit" value="Filter" /></p> 
        }
</p>

Any help would be greatly appreciated as I am brand new to all of this.

Thank you!

Was it helpful?

Solution

The problem was that variable Age did not have a pre-made {get; set;} it was manually mapped like this

public DateTime BirthDate { get; set; }
        public int Age
        {
            get
            {
                DateTime today = DateTime.Today;
                int age = today.Year - BirthDate.Year;
                if (BirthDate > today.AddYears(-age)) age--;
                return (int)age;
            }

        }

Thus Age did not exist in the table, it is considered non-mapped, and you must use a different mapped item (a variable with a {get; set;}) to determine your non-mapped variable.

In this scenario I used BirthDate, to calculate my Age within my controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top