mvc razor default value on httppost for unselected radiobuttons - asp.net-mvc

i have 2 radio buttons , which are optional, so by default no radio button is selected, if the user wants he can select the radio button. But on postback i should check whether the parameter entered by the user are same as the parameters defined in the application, for security.
There are other fields n the screen which are optional like textboxes, but textboxes are getting passed on httppost even when the user does not enter a value it is being passed as empty, but if user does not select radio button nothing is getting passed on postback, that field is missing in httppost.
Any work around to pass default value for unselected radio button on httppost.

You could place a "default" radio button, already selected, but not visible on your site ("style: display: none;"). That's the way ASP.NET works when you use some Html helpers like #Html.CheckBox(...) for instance: it places an invisible second checkbox with the same name, but containing the false value. It assures that a value will always be posted, even if the user doesn't check it (checkboxes, radio, etc are never sent with the form if they are not checked).

Related

Value from dropdown on IconTabBar empty

change view (XML) contains IconTabBar with 3 IconTabFilters, which contain input controls (input, combobox, datePicker...). These input controls have pre-filled value from OData model. On the bottom of the view is button "Save". When I click on "Save" button, in my "onSave" function I am reading values from input controls from all IconTabFilters, but only values from the input controls on the first IconTabFilter are filled. Values from the rest of the fields are empty.
When I click on all IconTabFilters (without changing values), click "Save", then I'm getting all values correctly.
Please what I'm doing wrong? Odata model contains all required values, and also IconTabFilters contain all required values. But I can't read them from input controls before clicking on all IconTabFilters.
The Controls on the tabs only get initiated when they have to be displayed. This is done to improve the felt performance of the UI.
Since you are already using model binding, you should take the values out of the model instead of the input fields.

Catching the name of a button during post

If I have an <input> of type submit, with a name, I can catch that name in my MVC model when I use it to post. Is the same not possible with a <button>, of type submit and the same name?
If I use for example name="ButtonName", my property ButtonName get's the input's value. For some reason this doesn't work with a button element.
The reason I want to use a button instead of input, is that i need to have more than text inside the button (including a picture).
You could set the value of the hidden input by using a simple onclick Javascript function for each of your inputs.
function setHidden(sender){
document.getElementById("hidden1").value = sender.value;
}

ViewBag values accessibility in mvc

The controller in which I define a viewbag value, returns a partial view, which is the last view that is beinng returned.
This partial view is rendered to a JQuery dialog. After I close it, I return to the one before, which contains a form. In the form view (the one before the last one) I'm trying to access the viewbag value via JS function, and assign it to a hidden field in the form. So actually, I'm trying to get the viewbag value, not from the view to which I've sent the viewbag.
Is it a problem? Are viewbag values available only from the last view that has rendered?
I'm not sure if it's possible, but in any case you wouldn't normally want or need to access ViewBag outside of the view it belongs to. It seems like there would be a couple of better options.
populate the ViewBag value you need into a hidden field somewhere else in the DOM.
Since you're using JQuery dialog, consider using its callback function to pass the values you need back to the main form. See here for an example: jquery ui dialog box need to return value, when user presses button, but not working

How to know which radio button is selected on a view?

I have a created a list view for one of my controller's index action.
I have added a new column "Select This" to this list view, by using the view's source.
The column will contain radio button for each entry in the list. This i have achieved by just placing a radio button control from tool box in the "Select This" column. This i have done in the design of the view and when i run it, i get radio buttons, one for each of the entry. The page also has a link button and i want to call a controller action on this link button click which will receive the index of radio button selected. So if i select 5th radio button, how can receive 5 in the controller action.
How can i do this?
Regards,
kapil
If you used plain HTML, your radio buttons will all have the same name, but different value. You will get one result in the POST, which will contain the selected value.
If you used ASP.net helpers, it's the same, but they will be generated for you: see here: http://msdn.microsoft.com/en-us/library/dd410596.aspx You will pass the same name (first parameter) and different values for each input (second parameter).

ModelState.IsValid for invisible controls

I am working on MVC with C#.
I have 2 radio buttons. On selecting first radio button, a textbox will be shown which allows to enter date values.
<%= Html.TextBox("ReceivedDate")%>
on selecting the second radio button, the textbox gets hidden.
For the first time, when i select first radio button and entered date and clicked Next to navigate to next page and came back to this page again and clicked second radio button and clicked Next to continue and again i came back to this page and without changing any option click continue, its not allowing to navigate and shows an error.
A value is required.
Which means the ModelState validating the hidden controls also.
Please suggest how to control it
Instead of hiding it remove the element from the DOM and reinsert it if the first item is selected again. Another way would be to change the name of the input control to something else (a key not present in your model data) when the first item is not selected.
Validating hidden input types is a good thing, i often use them to synchronize data from complex controls (like a treeview with checkboxes). An input type with a hidden css style doesn't make it not submit with the form it belongs too.

Resources