I have an MVC view that contains a JQuery UI dialog that can be opened and populated with data.
<div id="dialog">
.... Table of phone numbers
</div>
<div id="personData">
... Person model data
</div>
I am attempting to pass the data from the JQuery UI dialog along with the rest of the MVC View data to a controller action.
public ActionResult Save(Person person, List<PhoneNumber> phoneNumbers)
{
}
In this example the Person type is not part of the dialog and is posted fine. The phone numbers is in the div of the JQuery UI dialog and does not post.
The elements in the dialog are defined in the View and can be seen in the DOM but for some reason something is preventing the data to post along with the rest of the View data. If I remove the .dialog() declaration from the the "dialog" div (now the div is visible on the form), the data (phoneNumbers) will post.
So my question is how do you post the JQuery UI dialog data along with the View data from the form to a controller action? (I know how to post the UI dialog data using a button within the dialog, but I need to post it alongside my main View because of the state issues around this data).
Chances are the dialog box moves the div outside of the form so the fields aren't submitted. You might just link the fields to hidden ones outside of the div or increase the span of the form.
Just as stimms said, you will need to put the data into some fields inside the form you'll be submitting to the action.
Do something like this inside the dialog click function (using jQuery .val())
$("#formFieldName").val() = $("#dialogFieldName").val();
Repeat this for each field.
After that, the fields (probably hidden fields) inside your form will contain the data when you submit the form.
Related
I have to make a textbox somewhere in the page and a button using ASPX View Engine.That page already contains partial views. User will enter his data (code) on textbox and at button click, server code will manipulate that data and show an alert or message that data is processed. How to do that in mvc4?
I have found that we can write server script in page like
<script runat="server">
Protected Sub Button1_Click(sender As Object, e As EventArgs)
ScriptManager.RegisterStartupScript(Me, [GetType](), "showalert", "alert('Message Sent');", True)
End Sub
Private Sub DataProcess(ByVal Data As String)
'Code to check data
End Sub
</script>
You can create a separate Ajax Form ccontaining only this button and the textbox. So you will call an Action in a certain controller then this Action will return a string or a Json. And via an UpdateTargetId property you can display a dialog box. Giving you the code is difficult because I don't know the structure of your code. You can aslo write the Ajax call using Jquery.
This link can help you
I have a ASP.NET MVC application which has a view populated with a model which needs to retrieve some data from the user. Besides that, I also have a hidden div tag which contains more information but it should be visible only after the model was sent to the controller on a POST request and after it has processed the information from the model it should change the div tag to visible. Is some way to signal the view that the request was processed and render visible the div tag, remaining on the same page. I believe this is similar to a partial postback from ASP.NET.
Thanks,
Tamash
Yes that's possible via some AJAX functionality. I'm using jQuery in my example:
$.post('Controller/Action', $('#formId').serialize(), function() {
$('#yourDiv').show();
});
This uses jQuery to post the data contained in a form with the HTML id 'formId' to a URL 'Contoller/Action' and shows the div with id 'yourDiv' in case the AJAX request finished successfully. The call $('#formId').serialize encodes the form elements in HTML form with id formId for submission in the AJAX request.
More on jQuery and AJAX here
I am using a Jquery UI modal form to post data back to my action and it works fine. However when I put client side validation on and the user closes the modal without submitting the form retains the validation messages and styles.
It there away to clear the validation messages on the client? What element is the message wrapped in?
You could use the .resetForm() function:
var validator = $("#myform").validate({
...
...
});
And later when you close the modal dialog:
validator.resetForm();
I'm new to ASP .NET MVC and to web programming in general.
I'm wondering if there is a way to set ViewData variables when a radiobutton is selected -- but before the page is submitted.
Maybe I'm barking up the wrong tree but what I'm trying to do is create a form where new fields are added based on which radio button is selected. So what I want to do is when a radiobutton is clicked it sets a ViewData variable and based on that ViewData variable a different partial view loads the appropriate fields below the current field.
I imagine there must be someway of doing a onclick="some C# function that sets ViewData(args)"
Thanks
There are a couple of ways you could go about this.
1) You could have an Ajax form where through Javascript you post the form back and check to see if it's an Ajax Request, there by returning a partial view to a div that you specify.
2) Post the form as is and check server-side to see if the radio button was clicked, and thus redisplay the form with the new options visible.
If you take the first approach it would be easy enough to fall through to the second one for those without Javascript enabled.
There aren't really "onclick" events as I'm assuming you are used to from Webforms, you would basically have to roll your own Javascript to handle such things. Once you do a few, I think you'll find it's really not too bad, with the benefit that you'll have more control over what you're doing and through that gain a better understanding of the larger picture.
ViewData only exists, and only exists server-side, for the lifetime of the request. So, once the page is rendered the object no longer exists.
Some alternate approaches you can take:
1 - Use client-side Javascript to add a form and inputs as necessary. More info here:
ASP.NET MVC & JQuery Dynamic Form Content
2 - Pre-render the new form, but hide it via CSS, and unhide it when the appropriate radio button is clicked. More info here:
expand collapse html field Firefox
3 - Use AJAX to render the new form when the appropriate radio button is clicked. More info here:
http://www.asp.net/learn/mvc/tutorial-32-cs.aspx
I have an application where I need the user to be able to update or delete rows of data from the database. The rows are displayed to the user using a foreach loop in the .aspx file of my view. Each row will have two text fields (txtName, txtDesc), an update button, and a delete button. What I'm not sure of, is how do I have the update button send the message to the controller for which row to update? I can see a couple way of doing this:
Put each row within it's own form tag, then when the update button is clicked, it will submit the values for that row only (there will also be a hidden field with the rowId) and the controller class would take all the post values as parameters to the Update method on the controller.
Somehow, have the button be scripted in a way to send back only the values for that row with a POST to the controller.
Is there a way of doing this? One thing I am concerned about is if each row has different names for it's controls assigned by ASP.NET (txtName1, txtDesc1, txtName2, txtDesc2), then how will their values get mapped to the correct parameters of the Controller method?
You can use multiple forms, and set the action on the form to be like this:
<form method="post" action="/YourController/YourAction/<%=rowId%>">
So you will have YourController/YourAction/1, YourController/YourAction/2 and so on.
There is no need to give different names to the different textboxes, just call them txtName, txtDesc etc (or even better, get rid of those txt prefixes). Since they are in different forms, they won't mix up.
Then on the action you do something like
public ActionResult YourAction(int id, string username, string description)
Where username, description are the same names that you used on the form controls (so they are mapped automatically). The id parameter will be automatically mapped to the number you put on the form action.
You can also have multiple "valid-named" buttons on the form like:
<input type="submit" value="Save" name="btnSave" id="btnSave"/>
<input type="submit" value="Delete" name="btnDelete" id="btnDelete" /
and than check to see what submit you have received. There can be only one submit action sent per form, so it is like all the other submit buttons did not actually existed in the first place:
if ( HttpContext.Request.Form["btnDelete"] != null ) {
//Delete stuff
} elseif ( HttpContext.Request.Form["btnSave"] != null ) {
//Update stuff
}
I also think that you can implement a custom ActionMethodSelectorAttribute like here http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx (also listed above) to have cleaner separated code.
As rodbv said you want to use seperate <form> elements.
When you are using Asp.Net MVC or classic html (php, classic asp, etc) you have to forget the Asp.Net way of handling button presses. When a form is posted back to the webserver all the server knows about is simply "the form was sent, and contained the following input elements".
Asp.net (standard) adds a wrapper round many of the standard html postback actions using javascript (the __doPostback javascript function is used almost everywhere) and this adds information about which input element of the form caused the postback and delivers it to the server in a hidden form variable. You could mimic this behavior if you really so desired, but I would recomend against it.
It may seem strange 'littering' a page with many <form>'s, however it will mean that the postback to the server will be lighter weight and should make everything run that little bit faster for the user.