Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Different Views From MVC Controller

I've a MVC application, whose SharedLayout view(Master Page) gives user capability to search. They could search their order by Order No or By Bill no. So there are two option buttons the Shared View along with the textbox. Code is somewhat like this

   @using (Html.BeginForm("Track", "Tracking", FormMethod.Post))
        {
            <div style="text-align: center">
                <textarea cols="20" id="txtNo" name="txtOrderNo" rows="2" ></textarea>
            </div>
            <div style="text-align: center">
                <input type="radio" name="optOrderNo" checked="checked" value="tracking" />Order No                    <input type="radio" name="optRefNo" value="tracking" />Ref No
            </div>
            <div style="text-align: center">
                <input type="submit" value="Track" />
            </div>
        }

So it'll go to TrackingController and Track Method in it and return the view. It works fine for a single search as a View is associated with a controller's methods. It works fine but how could i conditionally return the other view based on the radio button selection.

What i come up with is this

 [HttpPost]
    public ActionResult Track(FormCollection form)
    {
        string refNo = null;
        if (form["optRefNo"] == null)
        {
            string OrderNo = form["txtOrderNo"];
            var manager = new TrackingManager();
            var a = manager.ConsignmentTracking(OrderNo);
            var model = new TrackingModel();
            if (OrderNo != null)
                model.SetModelForConsNo(a, consNo);
            return View(model);
        }

        refNo = form["txtConsNo"];
        return TrackByRef(refNo);
    }

    public ActionResult TrackByRef(string refNo)
    {
       //what ever i want to do with reference no
        return View();
    }

Kindly guide. Thanks

like image 543
manav inder Avatar asked Jun 18 '12 07:06

manav inder


1 Answers

View has an overload where the first parameter is a string. This is the name (or path) to the view you want to use, rather than the default (which is a view that matches the action's name).

public ActionResult TrackByRef(string refNo)
{
   //what ever i want to do with reference no
    return View("Track");
   // or, if you want to supply a model to Track:
   // return View("Track", myModel);
}
like image 145
moribvndvs Avatar answered Oct 03 '22 08:10

moribvndvs