0

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 :)``

2 Answers 2

1
  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.
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, i just cant get it how the post send the searchstring to the controller :S
0
  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.

6 Comments

Thats what i mean, that POST send the searchString right? But when it works for example with PHP i can acces the value with $_post[name] here i want to know where is that 'name'. I hope i did the right explanaition
@caxinaswin - based on your comment, it seems you're confused about model binding (or parameter binding). In this case, the MVC framework is looking at your posted values, and sees that you're posting a value with the same name as your parameter, thus it creates a parameter and fills it with the value automatically.
True, i am a little confused about MVC im learning really slow, and some tutorials dont explain really well little things, Soo what you say is that if i create a input = "text" in the view index and submit the form he submit the form in the same page and this time he send the searchString as parameter??
@caxinaswin - If you create a text input that has a name of "searchString" then it will populate the parameter of the posted to controller of the same name. Likewise, you can create a text input with the name "cocopuffs" and if you have a string parameter named "cocopuffs" then it will also pass the value to that.
@Html.TextBox("SearchString") soo this is responsable to pass the SearchString?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.