I’m trying to do a research boot on my view, but tell me it can’t be null

Asked

Viewed 65 times

-2

I’m trying to make a search button on mine view of films, but gives me a mistake to say that I can’t have a zero value. Why? And what do I have to do?

Controller:

 public ActionResult Index(string searchBy, string search)
    { 
        MovieViewModel[] movies = db.MoviesData.Select(movie => new MovieViewModel
        {
            MovieID = movie.MovieID,
            MovieName = movie.MovieName,
            MovieDescription = movie.MovieDescription,
            MovieCategory = movie.MovieCategory,
            MovieYear = movie.MovieYear
        }).ToArray();

        if (searchBy == "Categoria")
        {
            return View(movies.Where(x => x.MovieCategory==search || search == null).ToList());
        }
        else
        {
            return View(movies.Where(x => x.MovieName.StartsWith(search) || search == null).ToList());
        }
    }

View part of the search button:

<p>
@using (Html.BeginForm("Index", "Movies", FormMethod.Get))
{
    <b>Search By: </b>@Html.RadioButton("searchBy", "Nome", true) <text> Nome </text>
    @Html.RadioButton("searchBy", "Categoria") <text> Categoria </text> <br />
    @Html.TextBox("search") <input type="submit" value="Search" class="btn btn-default" />
}
</p>

The mistake you make is this::

'System.Argumentnullexception: The value cannot be null. Parameter name: value'

  • 1

    What is the exact line on which the error occurs?

  • 1

    @Ronaldoaraújoalves gives error in this: Return View(Movies. Where(x => x.MovieName.Startswith(search) || search == null). Tolist());

  • 1

    Want to ride a Minimum, Complete and Verifiable Example with the real situation, otherwise a solution beyond what has been presented.

2 answers

2


The mistake is certainly here:

return View(movies.Where(x => x.MovieName != null && (search == null || x.MovieName.StartsWith(search))).ToList());

I put in the Github for future reference.

I do not know if the logic of this is correct, but the adopted form will give error. I’m also not going to get into the merit that’s probably modeled wrong. The point is that if the data that comes is null you cannot call anything in it, for example you cannot call StartsWith() because there is no valid object there to execute something. So you have to prevent this data from being compared. I can’t say that I did it right because the question doesn’t make it clear what I should do. I reversed the comparison also because if I pass a null for the method StartsWith() will also give error.

  • is giving the same error as in the comment below. The model item passed into the Dictionary is of type 'System.Collections.Generic.List`1[Webapplication3.Models.Movieviewmodel]', but this Dictionary requires a model item of type 'Webapplication3.Models.Movieviewmodel[]' Movieviewmodel is where I have all movie properties

  • 2

    Here is another problem elsewhere. What you asked has been solved. Each question must contain a problem. But before you solve this you need to understand what you want, and post on the question, people can’t guess what you need, not just post the code. A full question has all the necessary information. Without knowing what you need any answer will be wrong, because even if it works, it won’t be right, working by coincidence isn’t right.

  • 2

    So the question no longer said on which line was the mistake, now it doesn’t say what it wants to do. You need to understand what programming is before programming. You are generating a data list, is this what you want? At view is dealing with a data, is this what you want? They are mutually exclusive, or take a data or list all data, but again this is another question.

  • I’m sorry, I’m very new to stack overflow, in my head what I was posting was correct, I’ll have more attention regarding my questions. I will then explain my goal. In my view there is a list of films that comes from the database. And on top has a button, 2 radio Buttons(to choose whether to search name or category) and a text box for the search. What I want this part of the show to do is to research films, by name or category. The error that appeared, before putting the search code did not appear. Again, I apologize.

1

The problem is because the search is null.

Just reverse the order. Of

x => x.MovieCategory==search || search == null

Change to

x => search == null || x.MovieCategory==search

The reason is that the StartsWith launches an Exception if the value passed is null.

When it’s done ||, o . net first checks the condition prior to the || and then the latter. If the former already meets (in the case of the OR, it is true), he does not pass by the second.

More details on operators || and |

  • But this gives an error in 'x', in '=>' and again in 'x'

  • Did you use it like that? return View(movies.Where(x => search == null || x.MovieName.StartsWith(search)).ToList());

  • It gave a much bigger error now: System.Invalidoperationexception: The model item passed into the Dictionary is of type 'System.Collections.Generic.List`1[Webapplication3.Models.Movieviewmodel]', but this Dictionary requires a model item of type 'Webapplication3.Models.Movieviewmodel[]'.

  • 1

    Well, that’s another mistake. Probably your view expects one MovieViewModel[] but you are passing a list (after all, you are using the .ToList(). But I would suggest creating a question for each different error, this facilitates future users who have the same problem finding the solution more easily.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.