when click on submit button debug pointing to [httpget] instead of [httppost] - asp.net-mvc-3

I have created one page using MVC3. I have filled all the details and when I click the submit button it's not doing inserting, just postback and all the details gets lost.
This is controller
[HttpGet]
public ActionResult MyAccountIndex()
{
return View();
}
[HttpPost]
public ActionResult MyAccountIndex(MyAccount myacc)
{
MyAccount ma = new MyAccount();
var objview = ma.GetCustInfo(myacc);
return View();
}

Change your form in View to:
<form action="MyAccountIndex" method="POST">
<input type="submit" />
</form>

Related

Using submit and text to pass a value to a controller

I am a begginer with asp.net mvc 3. I want to create a page where an user can enter a search item and retrieve the data from database. I have created the database. I have created the controller which is:
public ActionResult SearchIndex(string id)
{
string searchString=id;
var search = from m in db.Searches
select m;
if (!String.IsNullOrEmpty(searchString))
{
search = search.Where(s => s.Name.Contains(searchString));
}
return View(search);
}
I want to pass the value from view page to the above controller. What should be the code in view page to enter an item to search from above mentioned controller?
So create your view with a form inside... something like this:
#using (Html.BeginForm())
{
<label>Search:</label>
<input type="text" name="searchString" />
<input type="submit" name="submit" />
}
Then in your controller you can use the FormCollection object to grab your searchString like this:
[HttpPost]
public ActionResult SearchIndex(FormCollection formCollection)
{
string searchString = formCollection["searchString"];
...
}

Submitting form in partial view calls wrong controller action

I have a partial view and the corresponding controller which looks as follows:
public class SearcherController : Controller
{
SearcherViewModel searcherVM;
//
// GET: /Searcher/
public PartialViewResult Index()
{
searcherVM = new SearcherViewModel();
return PartialView("_SearcherPartial", searcherVM);
}
public ViewResult Search(FormCollection formCollection)
{
return View();
}
}
On the master page I render the partial view as follows:
#{Html.RenderAction("Index","Searcher");}
In my partial view I have a form with some inputs. Form SHOULD call Search action in the Searcher controller:
#using (Html.BeginForm("Search", "Searcher"))
{
...some inputs
<input type="submit" value="SEARCH" />
}
However when I click on the "SEARCH" button, the "Index" action of the SearchController is called. What am I doing wrong?

ASP.NET MVC 3 - Posting a Form Back to the Controller

I'm new to ASP.NET MVC. I'm currently using ASP.NET MVC 3. I'm trying to create a basic form with an image button that posts data back to the controller. My form looks like the following:
MyView.cshtml
#using (Html.BeginForm())
{
#Html.TextBox("queryTextBox", string.Empty, new { style = "width:150px;" })
<input type="image" alt="Search" src="/img/search.png" value="Search" name="ExecuteQuery" />
}
MyController.cs
public class MyController: Controller
{
public ActionResult Index()
{
ViewData["CurrentDate"] = DateTime.UtcNow;
return View();
}
[HttpPost]
public ActionResult ExecuteQuery()
{
if (ModelState.IsValid)
{
return View();
}
}
}
I have verified that I'm accessing my controller code. The way I've been able to do this is by successfully printing out the "CurrentDate" associated with the ViewData. However, when I click my image button, I was assuming that my break point in the ExecuteQuery method would fire. It does not. What am I doing wrong? How do I do a simple POST in MVC?
Your form is pointing to the wrong action.
Calling Html.BeginForm() creates a <form> that POSTs to the current action (Index).
You need to pass the action name to BeginForm.
Try this one
#using (Html.BeginForm("ExecuteQuery", "MyController", FormMethod.Post))
{
#Html.TextBox("queryTextBox", string.Empty, new { style = "width:150px;" })
<input type="submit" value="Search" />
}
In your controller
public class MyController: Controller
{
public ActionResult Index()
{
ViewData["CurrentDate"] = DateTime.UtcNow;
return View();
}
[HttpPost]
public ActionResult ExecuteQuery(string queryTextBox)
{
if (ModelState.IsValid)
{
// DO SOMETHING WITH queryTextBox
return View();
}
}
}
If the image submit button is very important for you, you can still change the button background image using CSS.

multiple submit button (asp.net mvc3)

I use two submit button. (asp.net mvc3 aplicattion)
I found how make it here:
http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx
Where put this The MultiButtonAttribute class? In Controller?
Maybe is easiest way to make this.
you can add it anywhere in application including Controller Folder
**//model**
public class input_element
{
public string Btn { get; set; }
}
**//views**
#using (Html.BeginForm())
{
<button type="submit" name="btn" value="verify">
Verify data</button>
<button type="submit" name="btn" value="save">
Save data</button>
<button type="submit" name="btn" value="redirect">
Redirect</button>
}
**//controller**
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
[HttpPost]
public ActionResult About(input_element model)
{
if (model.Btn == "verify")
{
// the Verify button was clicked
}
else if (model.Btn == "save")
{
// the Save button was clicked
}
else if (model.Btn == "redirect")
{
// the Redirect button was clicked
}
return View();
}

call javascript method from mvc3 controller?

i have a button on the cshtml view..its clicked every-time an item is scanned.
The user has to do it one by one and once all the items have been scanned..i want to opem/pop up a new window plus redirect him to another page.. The condition whether it was the last item..is being checked in the controller method.
How can i call a javascript to open the new window from the controller..right before my 'redirecttoaction' ?
is there a better way to do it?
Here's a sample pattern:
public ActionResult Index()
{
var model = new MyViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
// TODO Process the scanned code model.Code
if (IsLastItem())
{
model.IsLast = true;
}
return View(model);
}
and inside the view:
#model MyViewModel
#using (Html.BeginForm())
{
#Html.TextBoxFor(x => x.Code)
<input type="submit" value="OK" />
}
<script type="text/javascript">
#if (Model.IsLast)
{
<text>
window.open('#Url.Action("foo")', 'foo');
window.location.href = '#Url.Action("bar")';
</text>
}
</script>
Its not clean to call JavaScript from controller. Instead, move the logic of checking if its a last item to the client side and call appropriate controller action as appropriate.

Resources