Returning various views from an MVC controller

I have an MVC application whose SharedLayout view (main page) provides the user with the ability to search. They could search their order for order no. Or for bill no. Thus, there are two Option Shared View buttons along with a text box. The code is somewhat similar to 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 will go to the TrackingController and Track Method into it and return the view. It works great for a single search, since the view is associated with controller methods. It works fine, but how can I conditionally return another view based on the choice of the switch.

What I came up with is

 [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

+5
source share
1 answer

View , . ( ) , , ( , ).

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);
}
+15

All Articles