caxinaswin
caxinaswin

Reputation: 71

Some Problems with ASP.NET MVC

well, it's been a few weeks since I started programing in ASP.NET MVC and i came across with several issues, i have followed the tutorial microsoft introduction in this link:

http://www.asp.net/mvc/overview/getting-started/introduction/adding-search

there are 3 things that i cant understand very well

1 - In the example about the search in ASP.NET i cant get it how the searchString is passed from the form in the view to the controller

public ActionResult Index(string searchString) //this searchString in the controller
{           
    var movies = from m in db.Movies 
                 select m; 

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

    return View(movies); 
}

Razor Syntax

@using (Html.BeginForm()){    
     <p> Title: @Html.TextBox("SearchString") <br />   
     <input type="submit" value="Filter" /></p> 
    } 

Where the view send the string to the controller???

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

what is this strange syntax, where can i learn it, what means that m ?? i learned sql syntax and it is a little different from that :S.

3- the lambda expressions in this case how it works ???

for exemple: movies = movies.Where(s => s.Title.Contains(searchString));

well what i really need in this case is that someone explain me what i asked and how the flow in this case works cause its kinda confusing thanks :)``

Upvotes: 0

Views: 642

Answers (2)

Anton Norka
Anton Norka

Reputation: 2322

  1. Before Controller will be called, MVC framework parse form's POST and try to map fields and query parameters to method arguments automatically. Yes, it is really cool!!!

  2. It's LinqToSql. Try to find this key word. First I found is this one

  3. Actually this is Linq expression. s is a some record in movies dataset and s.Title.Contains(searchString) will filter movies by Title. When expression will become like s => true - that record is matched and will be added to expression result.

Update 01:

May I suggest to use

@Html.TextBoxFor(model => model.SearchString) 

as an alternative for your

@Html.TextBox("SearchString")

Because it's more about what for all that MVC we use. In common way Contoller will pass to View some model (actually it's just simple class) with filled SearchString property. At the top of your view you will have something like that:

@model SomeNamespace.SomeModelClass

It describes model for your view.

In Controller you will have:

var model = new SomeModelClass(){SearchString = "something"};
return View("Index", model)

I believe that's it.

Upvotes: 0

Ivan Gritsenko
Ivan Gritsenko

Reputation: 4226

  1. There are certain conventions applied here. Html.BeginForm() will produce a form tag with action attribute pointing to /ControllerName/Index, or simply /ControllerName assuming that Index can be omitted. You can notice another convention-thing here return View(movies);. The view will be first searched as Views/ControllerName/ActionName.cshtml.

  2. This is query syntax of LINQ. It is very similar to SQL.

  3. Again LINQ in action. Here we have an example of method syntax.

Upvotes: 1

Related Questions