dimanche 15 mai 2016

passing List object that created in controller to Index view

I have a problem to create a advance search in mvc, because I am new in mvc so I made some mistake in code in my question (but I made a lot of complicated sit in asp.net in vb or c#), I fixed my code with Mark's link (http://ift.tt/1ySIobw) , because some friend answer to my first wrong question that was solved by my self and it was confused I decided to ask new situation of my problem.

Actually now my search is working probably, I summarize the code as below:

My search view is:

@model IEnumerable<WomenNew.Models.News_>
@{
    ViewBag.Title = "AdvanceSearch_";
}
<h2>AdvanceSearch_</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.TextBox("Details", "this is value")
     <input type="submit" value="Search" />
}

In controller this view call it's method:

    [HttpPost]
    public ActionResult AdvanceSearch_(string Details)
    {
        //List<News_> news_ = new List<News_>();
        if (Deatails != null)
        {
            newsList = db.News_.Where(x => x.Deatails.Contains(Details)).ToList();
            return RedirectToAction("Index", "News_");
        }
        else
            return View("AdvanceSearch_");
    }

I had to define a public static variable because I don't know the correct way and I ask this question to know how the correct is.

public static List<News_> newsList = null;

And in my ActionResult Index I add a condition to know either newesList is null otherwise I pass the ListObject to Index view and sear work fine

    [HttpGet]
    public ActionResult Index(string searchBy, string search )
    {
        if(newsList != null)
        {
            return View(newsList);
        }
      .....
     }

I tried to not define public Variable but with passing between AdvanceSearch_ and Index the variable became null even ViewBag I checked it became null, I thought in protected override void Dispose(bool disposing)

method this is happened but I checked in another point it changed to null One friend told

A public static field is a single memory location available to every user of the application. Every user shares the same value.

I appreciate If anybody con help me how I can pass a List of Object from second block in this question (ActionResut Advance Search) to third block (Index ActionResult) or give me a link of code or a link of Walk trough for search in mvc which shows advance search with more fields. All I found out is a simple search inside the same view. A very kind friend (mgebhard) guide me to use TempData and he gave me a link :this is a tutorial mvc and is very valuable
Thanks a lot
Regards
Tomas

Aucun commentaire:

Enregistrer un commentaire