kendo ui button http post not firing - c#

In my index.cshtml page, following line of code DOES fire HttpPost so that I could get into the HttpPost tagged Index method in the controller class.
<input type="submit" value="Run Calculation" />
[HttpPost]
public ActionResult Index(TimberBeamCalculator.Models.Dimensions dim)
{
..................
}
But the following code for kendo ui button, does not fire the HttpPost mentioned above.
#Html.Kendo().Button().Name("setting").Icon("pencil").HtmlAttributes(new { type = "button" }).Content("Run Calculation");
What am I missing??

Kendo.button does not render out as a submit button. You either need to tweak the properties of your kendo button to make it a submit button, or use jquery to do something like
$("#setting").click(function()
{
$("#idOfYourForm").submit();
});
First, you should try setting the HtmlProperties of your kendo button with type of submit. If that doesn't work, go for the jquery route I mentioned.

Related

ASP.NET MVC: How do I change where Ajax.BeginForm will post to after the page loads?

I want to make an Ajax.BeginForm that will by default, go to the add user page. I also want in the same form an edit and delete button. The information in the form will be the same, I just need it to post to the different edit user and delete user URL.
How can I modify where the form will post, depending on what submit button is pressed, while keeping all the items that Ajax.BeginForm gives us.
Edit:
Also wanted to note that I want the URL to be generated by my routes. So in the same way the BeingForm uses the "action" and "controler" to make the path, I want to use that as well when I change the URL, so the URL is dynamic and not static.
I was able to get this to work by using jquery to change the action to the form using Url.Actions.
The HTML button:
<button type="submit" class="btn btn-danger" id="formDeleteButton" onclick="ChangeFormToDelete()">Delete</button>
The javascript/jQuery
function ChangeFormToDelete() {
$('#UserForm').attr('action', '#Url.Action("DeleteUser", "UserManagement")')
}
In #Url.Action the firstparameter is the action and the second is the controller. I made the exact same functions for edit and add and it worked perfectly.

mvc 3 - Automatically going to HttpPost

I have an action as follows:
public ActionResult ChangeFeeCheck(string id)
{
ViewBag.id = id;
return View();
}
on my View, I have the following:
#{
ViewBag.Title = "CreateList";
}
Please enter first name <br /><br />
#using (Html.BeginForm())
{
#Html.Textbox("firstname")
<input type="button" id="SaveChanges" value="Save" />
}
When I click on the button, I was expecting it to to the following
[HttpPost]
public ActionResult ChangeFeeCheck(string firstname)
{
.....
}
I am not sure when MVC will automatically go to the HttpPost or if I when need to manually have it there. In the above, it does not go there directly. I have to use the
window.location.href
and pass the url of the controller/action.
Meaning, isn't the default for
Html.BeginForm()
The HttpPost(same name as the HttpGet)
You need the button to be a submit button:
Change:
<input type="button" id="SaveChanges" value="Save" />
^^^^^^
To:
<input type="submit" id="SaveChanges" value="Save" />
^^^^^^
If you are following "Convention over configuration" rule over here, then the view you have created here must be for ChangeFeeCheck action, and ChangeFeeCheck here looks like will make compiler confused as no overload, same name, same signatures.
and then when method for form is get it would go to the first one, while if method for form is POST, it will call the one decorated with [HttpPost]
And because you are using submit button and by default HTML form generated uses POST action, it call the [HttpPost]
You can refer this article (from the internet archive as original link is now down): https://web.archive.org/web/20120527133344/http://microsoftmentalist.com:80/2011/09/07/asp-net-mvc-difference-between-httpget-and-httppost-with-example/
See for example how GET and POST action methods are overloaded.
First of all same signature same name method can't be compiled in same controller it will give you the compilation error already have member of same parameter type.
You have to differentiate these two similar name method by different signature.
Regarding HttpPost and HttpGet your get method will be called whenever you have to retrive data or your page load is called for that view.
HttpPost method will be called either you are using button of type submit or your input type is button but using jquery you are giving ajax call on button click and your ajax type is "Post"
$.ajax({
url: "Action"
type: "Post"
},succees: function(){alert('succeed');});

JQuery MVC3 How do I pass a parameter from a button submit and get the action result back

I have a form that has a combo box and a div underneath it. It has show and delete buttons next to combo box.
When someone selects an item in the combo box and then click on the Show button, I want the button to call an action in the controller and pass the selected value of combo box to the action. I then want it to load the view in the viewDiv div.
For example:
<input type="submit" value="Show" onclick="alert($('#SelectedId').val(););" />
<div id="viewDiv"/>
I want onclick to call a action in controller and pass the Id = $('#SelectedId').val(); and then populate the viewDiv with the contents of the view.
Please guide as I am very stuck
Try this. Assuming your submit button has an id = "btn" this should work:
$("#btn").bind("click", function(e){
e.preventDefault();
$("#viewDiv").load("yoururl", { id = $("#SelectedId").val() });
});
There are a lot of different ways you can accomplish this.
I would recommend wrapping all of your fields in a form. Then if you submit the form, your action can be called and any data on the form can be returned.
#using (Html.BeginForm("Login", "Account")
In MVC talk, you can use something like this to call a Login action on an Account controller.
This is the equivalent to
<form action="/Account/Login">
</form>
Now you can submit this form with jQuery or with a input type=submit button, etc.
The key to getting your variable to the action function would be to just name the combo box id the exact same name as the variable.
You can also return the entire model that's on the page by having the model's class as the class you are expecting
public ActionResult Login(LoginModel model);
You can also pass in custom variables:
#using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }
and
public ActionResult Login(string returnUrl);

ASP.NET with MVC 2 in VS2010

Hi
i want to do a work in asp.net using mvc and ajax
i have a button
when i click that button,its text should be changed.e.g before click(Click me) after click(u clicked me)
but i want to do this work in MVC2
i have studied but couldn't understood mvc
kinfly do this example so that i can understand it easily
Best Regards:
Shaahan
are u just trying to change the text label on click?
there's a few ways to do this, but you can probably just use an onclick event and change the label straight when the user click on the button.
for example like so.
but if you want to do it MVC just for the heck of it, then you can create a view, click on the button and do a form post to the same page, and on the controller use ViewData["ButtonLabel"] and update the button label when the page goes back :P
You can do what you want simply with javascript. If you want to learn mvc here a simple [music store][1] tutorial that I has helped me a lot! [1]: http://www.asp.net/mvc/tutorials/mvc-music-store-part-1
MVC stands for Model, View, Controller.
The way this works is you have a controller, say HomeController which is a class derived from Controller. When you access /Home/ on the site through your browser it serves the browser a view and any additional information, often cookies and such. The model is the data and logic of the program, often handling things like databases.
There are multiple ways to go about this example.
//In HomeController class
public ActionResult Index()
{
return View();
}
public ActionResult Clicked()
{
return View()
}
And then for the Views for the index view you'd have a button which would link to /Home/Clicked. Then on the clicked view you'd have the button with changed text.
Of course this is only one way to do it you could just append a number do the /Home/ url and pass that to the view and if it's not 0 have the test be different or use javascript to change the button's text
Index view:
<form>
<input type="button" value="NClicked" onclick="window.location.href='/Home/Clicked'">
</form>
Clicked view:
<form>
<input type="button" value="Clicked" onclick="window.location.href='/Home/'">
</form>
Of course there's more to the views than that, but you can insert that into your body.

MVC Post to another action

Strait to the details...
I'm working on a personal project basically it's a task list. Anyways; I managed to get the standard Add, Edit, Delete a task functionality going good; now I'm stuck on something that I know must be very simple. I would like for users to be able to accept a task from the details page, now I could easily put that option in a drop down list and allow the user to select it and then save; BUT i would like to just provide a link that they can click "Accept Task" that link then goes to my controller action and pulls the task then updates the TaskStatus field.
This is my controller action
//
// TaskStatus Updates
[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult AcceptTask(int id)
{
Task task = taskRepository.GetTask(id);
try
{
task.TaskStatus = "Accepted";
taskRepository.Save();
return RedirectToAction("Details", new { id = task.TaskId });
}
catch (Exception)
{
throw;
}
}
So now how do I call this action from within my "Details" view?
A small form containing a button that posts back to the AcceptTask action would work. It could even be an AJAX form, if you wanted.
<% using (Html.BeginForm("accepttask","task",new { id = Model.TaskId })) { %>
<input type="submit" value="Accept Task" />
<% } %>
A link (<a>) cannot post a form, so you have three choices:
Use a button instead;
Use an <input type="image"> instead, which you can finesse to look similar to a link;
Use JavaScript to do it.
There's already an answer posted for #1, which is easily modified to #2. I'll post a jQuery example for #3, although you can use any JS library for this:
<a id="acceptTaskLink" href="#">Accept</a>
<span id="accepted" style="display: none">Accepted</span>
...
<script type="text/javascript">
$('#acceptTaskLink').click(function() {
$.post('/site/task/accept',
function(result) {
$('#acceptTaskLink').hide();
$('#accepted').show();
});
});
</script>
This script makes an Ajax post to the controller, then after it successfully posts, it changes the "Accept" link into regular "Accepted" text (by hiding the link and showing a confirmation element, which is presumably in the same place).
A link should not perform any action by definition. It leads to strange problems: What happens when google crawls your site? What happens when somebody refreshes the page? What happens when somebody bookmarks the page?
Actions that change content are usuallly done by get after post. First post to the Action that changes something (AcceptTask) then redirect to a page that displays the result. In you case I would suggest for the get the list of tasks with a success message. For the Mesage to be available after redirect use Tempdata.

Categories

Resources