With all due respect, you really need to work through some MVC tutorials. I'm not usually a fan of Microsoft documentation (especially their insipid 'quickstarts') but their MVC tutorials are actually quite good. That said, let me clear some things up for you.
Firstly, the whole point of creating a view model (Search
in this case) is so that you don't have to use ViewBag
or ViewData
, to give you a guaranteed way of accessing your model's data. ViewBag
and ViewData
are virtually the same thing. The main point here though is the idea to move away from these in favour of using strongly-typed views.
So, that begs the question, what is a strongly-typed view? Put simply, a strongly-typed view is a view that requires a specific type to be passed to it. You specify a strongly-typed view by using the @model
directive at the very top of the view. In your case, this would be:
@model TheLibrary.Models.Search
Note: The lowercase m
. Do not confuse this with @Model
which is a way of accessing your strongly-typed model.
Being as we're telling our view exactly what type we're going to pass to it, we can use @Model
to access the properties of the model, like so:
<div>
@Model.searchCriteria
</div>
(Please also note that guidelines suggest using Pascal Case for properties.)
Now, let's clear up the issue with your controller. Firstly, as we're already using our model, as discussed above, there is no point in using ViewBag
or ViewData
. TempData
serves an altogether different purpose and has no bearing on your question. (See this answer for when you might want to use it.) In that sense, it is also not needed. Lastly, you would simply pass the model to the view itself:
public ActionResult Result(string text)
{
var model = new Search { searchCriteria = text };
return View(model);
}
Now, your complete view would look like this:
@model TheLibrary.Models.Search
<div>
@Model.searchCriteria
</div>
However, as stated at the start of this answer, I really believe you should look into a few tutorials because there are things here that you're still not aware of (such as passing searchCriteria
directly to your view without any kind of validation).
Update per comments
Your view wants to look something like this:
@using (Html.BeginForm("Result", "Search", FormMethod.Get))
{
@Html.TextBox("text")
<input type="submit" value='Search' />
}
There are a couple of things to note.
Firstly, the FormMethod.Get
is ensuring this form's data is sent via GET instead of POST. This is what puts text
into the query string in your result's URL. So it makes it look something like:
http://somewebsite.com/Search/Result?text=test
This is good because it means if the user refreshes the page, they're not asked to resubmit the form and it also acts in the same way every time the page is accessed from that URL.
Secondly, @Html.TextBox("text")
. The string you supply to the TextBox()
method is the name that is given to that textbox, and it is also the name that is used to lookup the data in that textbox. That means it has to match the name of the parameter in your Result
action (or part of a model, but let's keep things simple for now), so this part is important for it to work correctly.
So the idea here is that for you to get the correct data for your searchCriteria
in your Result
view, using your example, is that the user would type test
into the textbox. They'd then click the Search
button which would get MVC to call the Result
action. When doing so, it would use what's called 'model binding' to match the value of the text
textbox to the text
parameter in your action. That means when we get here:
public ActionResult Result(string text)
{
// ...
}
text
will have the value of whatever someone has typed into the textbox. You then pass that to your model which is then passed to the view.