8

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

1 Answer 1

21

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

Comments

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.