0

I am new to mvc 3. i am trying to create a view and controller. my scenario is that i have controller which has two actions. while i call first action inside controller i have to load default values to view. inside view i have a buttons to post back values. so while click one of the button inside view, it has to go second action in same controller and has to return back to same view with success or failure message.

My view is like this

@using (Html.BeginForm())
{
<table>
    <tr>
        <td>
            @Html.TextBox("txtTitle")
        </td>
        <td>
            <input type="button" value="Go Somewhere Else" onclick="location.href='@Url.Action("Postback", "Default1")'" />
        </td>
    </tr>
</table>

}

My Controller,

public class Default1Controller : Controller
{
    //
    // GET: /Default1/

    public ActionResult Index()
    {
// has to default load value to text box
        return View();
        }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Postback(FormCollection obj)
    {
// has to take value from txt box
        object obj1 = Request["txtTitle"];
        return View("Index");
    }
}

My problem if call Postback action from any other view it works. but inside same view if i click on button the error shows like "http://localhost:14953/Default1/Postback".

What is solution here? i expect to navigate to same controller as well as to other controller inside same form and return to same view.

Vetrivel mp
  • 1,214
  • 1
  • 14
  • 29

4 Answers4

2

As reference to this article.....

public class HttpParamActionAttribute : ActionNameSelectorAttribute {
  public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) {
      if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
        return true;

      if (!actionName.Equals("Action", StringComparison.InvariantCultureIgnoreCase))
        return false;

      var request = controllerContext.RequestContext.HttpContext.Request;
      return request[methodInfo.Name] != null;
  }
}

In view form like this...

@using (Html.BeginForm("Action", "Post")) { 
  <!— …form fields… -->
  <input type="submit" name="SaveDraft" value="Save Draft" />
  <input type="submit" name="Publish" value="Publish" />
}

and actions in controller...

public class PostController : Controller {
    [HttpParamAction]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult SaveDraft(…) {
        //…
    }

    [HttpParamAction]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Publish(…) {
        //…
    }
}
naim shaikh
  • 1,103
  • 2
  • 9
  • 20
  • Nice find. I can foresee instances where you might have conflits with button names and actions but I can see this as a workable solution. – Nick Bork Apr 26 '12 at 04:37
  • yes i also found the same solution it looks good if we handle inside same controller. what about to navigate another controller from current view? you should not create another form. – Vetrivel mp Apr 26 '12 at 04:41
  • Int that scenario you can place a simple button in you form. Use ajax to serialize and post any action and any controller... – naim shaikh Apr 26 '12 at 04:52
0

I think we can specific action and controller at Html helper.

@using (Html.BeginForm("Postback", "Default1"))
{
  <table>
     <tr>
      <td>
        @Html.TextBox("txtTitle")
      </td>
      <td>
        <input type="submit" value="Go Somewhere Else" />
        <!-- the input should be submit?-->
      </td>
</tr>

Pongsathon.keng
  • 1,417
  • 11
  • 14
0

You can specify an ActionName to your Postback method like this:

[ActionName("Index")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Postback(FormCollection obj)...

So F5 repost the same values and calling the URL directly from the address bar returns the View as expected.

labilbe
  • 3,501
  • 2
  • 29
  • 34
0

There is a constructor for Html.BeginForm Helper method which takes action name and controller name. Use that

public static MvcForm BeginForm(
    this HtmlHelper htmlHelper,
    string actionName,
    string controllerName
)

http://msdn.microsoft.com/en-us/library/dd492590.aspx

So your form should look like this. You dont need the onclick function in your input element.

@using (Html.BeginForm("Postback", "Default1"))
{
  //Your elements
 <input type="button" value="Go Somewhere Else"  />
}

That will render the HTML markup like this

<form action="/Default1/Postback" method="post">
 // Your elements
  <input type="button" value="Go Somewhere Else"  />
</form>

If you want multiple submit button in the same form, It is answered here

Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • i understand this solution but i have more than one button in side same view which calls different controller/action. if you give action in form tag this is tightly coupled with one controller action. – Vetrivel mp Apr 26 '12 at 01:39
  • @Vetrivelmp : check this http://stackoverflow.com/questions/8258750/asp-net-mvc-3-multiple-submit-inputs-in-one-form – Shyju Apr 26 '12 at 01:42
  • can not we give controller url directly to button onclick event? – Vetrivel mp Apr 26 '12 at 01:46