I'm trying to use the same view to display different lists based on the param I'm passing from the button in JSP.
what I need is: when user click on button, will call the same view but data should be display from the second list .(getPreviousYearReports())
here's my code:
#RequestMapping(value = "/viewReports.do", method = {RequestMethod.GET , RequestMethod.POST})
public ModelAndView viewReports( #RequestParam final Integer storeNumber,
final HttpServletRequest request) {
ModelAndView view = getModelAndView("viewReports");
List<StoreReport> reportsList;
if(request.getParameter("previous") != null ) {
reportsList = storeReportService.getPreviousYearReports(storeNumber);
} else {
reportsList = storeReportService.getReportsByStore(storeNumber);
}
....
}
in my JSP.
I have added submit button to call the same view mapping
<button id="previous" name="previous" type="submit" class="btnFwd gradient smButtonStyling"
onClick="window.location.href='/viewReports.do?storeNumber=${storeNumber}'">
View Previous Year Reports
I was missing to pass the "&previous" param
<button id="previous" name="previous" type="submit" class="btnFwd gradient smButtonStyling"
onClick="window.location.href='/StoreReports/viewReports.do?storeNumber=${storeNumber}&previous'">
View Previous Year Reports
</button>
I am a beginner and currently started a small project for my study purpose. A mini DB search portal. I created front end View. It has one search box and a button. Now what I need is, I have to fetch the data from DB, related to the user entered search term on clicking on the button.
How to proceed to get the user entered data from view to controller and process it for further operations.
General code:
view:
#using(Html.BeginForm("Search","Test"))
{
<input type="text" name="txtName"/>
<input type="submit" value="Generate report" />
}
Controller
[HTTPPost]
public ActionResult Seacrh(FormCollection form) // "Search" is action name specified in view for controller "TestController"
{
string text = form["txtName"]; // txtName is name of input in view.
}
However to take full advantage to MVC have a look at Model Binding in MVC
I recommend reading this article, it has an example different ways of adding search.
You can update the Index method in your Controller and View as follows:
1)Add a form to the view that will post to the view itself
View
#using (Html.BeginForm()){
<p> Title: #Html.TextBox("SearchString") <br />
<input type="submit" value="Filter" /></p>
}
2)Add a parameter to the Index method to filter the content based on the parameter passed. public ActionResult Index(string searchString)
Controller
var movies = from m in db.Movies
select m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
return View(movies);
I am just learning spring MVC.
I have passed a object named emp of class Employee using model.addAttribute("emp",emp); to a jsp page. I have used the same modelAttribute in form attribute of jsp as:
<form:form method="POST" modelAttribute="emp" id="emp" action="${pageContext.request.contextPath}/AcceptIncrementDecrementPayroll">
So the fields with matching are displayed and allowed to update. Suppose Employee has firstName and salary. The salary is displayed in a text box and is editable. So I need the new salary value in the text field along with the other properties like firstName as an object of the employee class.
These are the controllers:
This is the first controller where i am setting the model attribute to Employee object emp.
#Transactional
#RequestMapping(value = "/viewIncDecPay")
public String ViewIncrDecrPayroll(HttpServletRequest req,Model model)
{
List<Employee> empList=this.employeeDaoImpl.getCurrentCTC(staffId);
System.out.println("empList has: "+empList.toString());
List<IncrementDecrementPayroll> empRequestList=this.employeeDaoImpl.getExpectedCTC(staffId);
System.out.println("empRequestList has: "+empRequestList.toString());
int count=0;
for(Employee emp:empList){//empList has only one object as it is fetched based on a criteria
System.out.println(count+"in List");
model.addAttribute("emp", emp);
}
model.addAttribute("incrementDecrement", empRequestList);
return "ViewIncrementDecrementPayroll";
}
This is the jsp page Form declaration
<form:form method="POST" modelAttribute="emp" id="emp" action="${pageContext.request.contextPath}/AcceptIncrementDecrementPayroll">
<form:input path="salary" type="text" class="form-control" name="" id="basicMonthly" value=""/>
</form>
This is the controller function that is being hit by the jsp page
#Transactional
#RequestMapping(value="/AcceptIncrementDecrementPayroll")
public void acceptIncrement(HttpServletRequest req,Model model,#ModelAttribute("Employee")Employee emp){
System.out.println("in accept payroll");
String employeeName=emp.getFirstName();//this gives me null as all the properties are lost
System.out.println("returned after jsp: name is: "+employeeName);
}
Above in #ModelAttribute I have declared again the object and there it looses it properties, but I don't know how to call the same object again. Thanks for any suggestion!
I have some code in JSP as below:
<c:iterate name="list" id="payment" index="idx">
<tr class="gRowEven"
_paid="<c:write name="payment" property="paid"/>">
Now my problem is that I want to call a method in a controller based on the variable _paid. I can do a request.setAttribute("_paid", _paid)
I am assuming it would work. But I am not supposed to do it that way. So I was wondering if there is any other way to do it?
You can pass that value to a hidden input field (on form submit)
<form action='your controller' method='POST'>
...
<input type='hidden' id='newfield' name='newfield' value=''/>
</form>
then you controller can retrieve its value (using request.getParameter('newfield'), or a MVC framework provided method)
Or simply append to the URL if your controller takes GET request
By the way,
request.setAttribute("_paid",_paid);
probably won't work for you. Because this call is only executed when page is loaded, not when you submit the page. Also, when you submit a page, it will have a new fresh request
Edit: (this is what I meant by saying 'pass that value to a hidden input field (on form submit)')
<script type='text/javascript'>
function updateHiddenField() {
var trPaid = document.getElementById('trPaid'); //assume tr field's id is trPaid
//if tr doesn't have an id, you can use other dom selector function to get tr
//element, but adding an id make things easier
var value = trPaid.getAttribute('_paid');
document.getElementById('newfield').value = value;
document.getElementById('form1').submit();
return false;
}
</script>
<form action='your controller' id='form1' method='POST'
onsubmit='javascript:updateHiddenField();'>
...
</form>
Then _paid's value will be passed with request in newfield parameter
And here's how you get the parameter when you are using Spring MVC
#Controller
#RequestMapping("/blah")
public class MyController {
#RequestMapping(value="/morepath" method = RequestMethod.POST)
public ModelAndView test(#RequestParam("newfield") int paid) {
logger.debug("here's the value of paid: " + paid);
}
...
I'm using MVC3 Razor. I have 2 submit buttons setup on my view but the problem I'm having is that both submit buttons cause the validation of the model. I want to hook up individual submit buttons with specific input controls for validation.
I know this is a few months old but the solutions here seemed needlessly complex and there's no accepted answer yet. If you name your inputs the same but give them different values, you can get that value in your controller just by including a string with the name of the input as a variable. This is how I solved this problem:
View:
<input type="submit" id="EnterprisePush" name="btnSubmit" value="Push" />
<input type="submit" id="EnterprisePull" name="btnSubmit" value="Pull" />
Controller:
[HttpPost]
public ActionResult EnterpriseAdmin(int id, string btnSubmit, FormCollection collection)
{
switch (btnSubmit) {
case "Push":
/* Do Something here */
break;
case "Pull":
/* Do Something else here */
break;
}
The browser is always going to submit the entire form regardless of which submit button you press.
The best solution would be to have two submit buttons with the same value for the name attribute and different values for the value attributes.
When you submit the form, the value of the button will be submitted as well. In your action which handles that form submission, you check to see the value of the button and perform the correct validation based on that.
In your form you would have something like this:
<button type="submit" name="Command" value="command1">Do Command #1</button>
<button type="submit" name="Command" value="command2">Do Command #2</button>
Your Form Model would look like this:
public class MyFormModel() {
public string Command {get;set;}
public string SomeOtherVal {get;set;}
}
Your controller\action would look like this:
public ActionResult HandleFormSubmit(MyFormModel model) {
if (model.Command == "command1") {
// do something
} else if (model.Command == "command2") {
// do something else
}
}
Firstly, you can disable client validation on your cancel button simply by adding the CSS class 'cancel' to it. See: Disable client-side validation in MVC 3 "cancel" submit button
Secondly, as well testing the submit element's form name as described above, you can use a custom action selector. Here's mine, which I originally took from the blog post shown in the comment:
/// <summary>
/// Used to vary an action method based on which button in a form was pressed. This
/// is useful but is an anti-pattern because it couples the controller to names
/// used in the form elements.
/// </summary>
/// <remarks>
/// See the example at http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx
/// </remarks>
public class AcceptButtonAttribute : ActionMethodSelectorAttribute
{
public string ButtonName { get; set; }
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
var req = controllerContext.RequestContext.HttpContext.Request;
return !string.IsNullOrEmpty(req.Form[this.ButtonName]);
}
}
In your controller:
[HttpPost]
[ActionName("Edit")]
[AcceptButton(ButtonName = "Cancel")]
public ActionResult Edit_Cancel(MyModel model)
{
return RedirectToAction("Index");
}
[HttpPost]
[AcceptButton(ButtonName = "Save")]
public ActionResult Edit(MyModel model)
{
// do real work here
}
Note that you need the [ActionName("Edit")] attribute to tell MVC that although using a different method name, it is for the Edit action.
And in your View:
<input type="submit" name="Save" value="Save" />
<input type="submit" name="Cancel" value="Cancel" class="cancel" />
My solution was to do two things. Say we have a Save button and another Add Something button. When user clicks on Save we want client validation and server validation to be performed. For later button we don't want any validation to take place.
Temporarily disable client validation for second button (on click):
<input type="submit" name="submit-button" value="Save" />
<input type="submit" name="submit-button" value="Add Something" onclick="document.forms[0].noValidate = true; document.forms[0].submit();" />
Good thing about it is when JavaScript is disabled the client validation would never have taken place anyway.
Take care of server side
Similar to what Bryan is saying when you click any submit button within a form, the entire form and the clicked submit button value is posted. You can differentiate which button was clicked by the name posted. In example above when user clicks on Save button and we read Request.Form["submit-button"] in controller post action we get "Save". If user clicked on Add Something we would get "Add Something". This is the way HTML is supposed to work.
Now to get around having magic strings all over the place I usually have a public static class within the controller, like so:
public class HomeController
{
public static class Buttons
{
public const string Save = "Save";
public const string AddSomething = "Add something";
}
// Action methods
}
So you can use these for rendering form:
<input type="submit" name="submit-button" value="#HomeController.Buttons.Save" />
And you can easily read the button clicked in controller:
[HttpPost]
public ActionResult Index(Model viewModel)
{
var buttonClicked = Request.Form["submit-button"];
switch (buttonClicked) {
case HomeController.Buttons.Save:
return Save(viewModel);
case HomeController.Buttons.AddSomething:
return AddSOmething(viewModel);
}
return View();
}
In Save method you first ask if ModelState.IsValid and return view model if not but in AddSomething method we will clear any errors:
public ActionResult AddSomething(Model viewModel)
{
ModelState.Clear();
// your code to add something to model
return View(viewModel);
}
This was you keep everything clean, tidy and testable. And you can introduce a constant for submit-button html name attribute. It might be possible to do all the constants with T4MVC too. A similar solution applies to when you need a "auto postback" combo box, except you need a hidden field that is set via onchange event of the select element.
Hope this helps.
Just use this code as a template:
#{
var nextButtonVal = "Next >>";
var backButtonVal = "<< Back";
if (IsPost) {
if(Request["navigate"].Equals(backButtonVal)){Response.Redirect("~/pageFoo");}
if(Request["navigate"].Equals(nextButtonVal)){Response.Redirect("~/pagebar");}
}
}
<input type="submit" value="#backButtonVal" title="Back" name="navigate"/>
<input type="submit" value="#nextButtonVal" title="Next" name="navigate"/>
One final thing I would do is instead of using intelligent strings, use an enum to determine the value for each input tag. Using razor syntax:
#Enum.GetName(typeof(YourEnumType), yourEnum.WhateverValue)
then in your controller:
public ActionResult DoSomethingBasedOnEnumValue(string enumValue)
{
YourEnumType localVar = (YourEnumType)Enum.Parse(typeof(YourEnumType), enumValue);
switch(localVar)
{
case YourEnumType.Action1:
//do something
break;
case YourEnumType.Action2:
//do something else
break;
}
return View();
}
If you want to have separate action for delete, try this.
add a delete action in the controller and mark it as HttpDelete,
[HttpDelete]
public ActionResult Edit(int id, string foo) {
...
}
And in the view,
button name should be X-HTTP-Method-Override and value should be DELETE
<button name="X-HTTP-Method-Override" value="DELETE" formnovalidate="formnovalidate" class="cancel">Delete</button>
note: all most all the browsers don't allow for other HTTP methods, like HEAD, PUT, or DELETE. but by add a header to the HTTP request, X-HTTP-Method-Override, that is supposed to be interpreted by the service and acted upon regardless of the actual HTTP method used. So above code will add a header to the request like X-HTTP-Method-Override: DELETE. and .net framework will do the rest of the things and direct you to delete action.
Submit button name don't come to server side if in all from this situation you are will be use [Remote] attribute for validation model property.