-3

Is there any way to use many model properties in the same radio button group on html without destroying the Name field, which Razor uses to grab the unique field on submit?

I've tried the following:

<h2>Store or Corporate</h2>
        <div class="store-selection-buttons">
            <label>Corporate</label>
            @Html.RadioButtonFor(model => model.JobPostStatus.IsCorporate, "true")
            <label>All Stores</label>
            @Html.RadioButtonFor(model => model.JobPostStatus.IsAllStores, "true")
            <label>Select Stores</label>
            @Html.RadioButton("Store Selection", false, new { @id = "select-stores" })
            //This one does other selection work in jquery for individual stores (List<int>)
        </div>
Richard
  • 122
  • 12
  • 1
    I don't understand the sentence *however Razor only allows RadioButtonFor's to be used for parameters as the combining feature*? – Liam Jan 25 '16 at 16:02
  • The only way I know how to link the parameter to the radiobutton is the RadioButtonFor(), but then you can link the radio buttons together, since the name is then the parameter name – Richard Jan 25 '16 at 16:50

3 Answers3

1

You're attempting to utilize in your model two distinct properties:

  • IsCorporate
  • IsAllStores

The model indicates that both can be true or false at the same time. You should really group them to a single property, since only one can be selected at once. If you requite two separate you could append another hidden and go off both, but obviously you'll fight with "what if both are true?" which one should remain selected.

You could do this in several approaches though, the simplest would be:

<input type="hidden" value='@Html.DisplayFor(business => model.IsBusiness)' />
<input type="radio" name="businessType" value="true">Corporate</input>
<input type="radio" name="businessType" value="false">Store</input>

Then you could simply have jQuery on document.load trigger the change to select the proper value.

Another option would be if you transitioned to the single value like I mentioned, you could follow this Stack Overflow answer.

Community
  • 1
  • 1
Greg
  • 11,302
  • 2
  • 48
  • 79
  • Single value won't work in this case, the selection is a little different then a definitive yes/no. It's a Yes(All), No(All), Yes(Some in a list) type option. Converting to a simple Boolean or Enum wouldn't work in my instance – Richard Jan 25 '16 at 16:47
  • You couldn't tweak and MVVM to convert to a single for this one instance? – Greg Jan 25 '16 at 16:56
  • It's not possible at the moment to condense it down to a single parameter (without putting core logic on the view) – Richard Jan 25 '16 at 17:01
1

After researching it, Razor needs the names of the radio buttons to be distinct successfully post back, but the Radio Buttons themselves require the same name to be linked up successfully.

There is no current way of resolving this using Razor syntax and the best alternative was to use checkboxes with if() statements turning off all the other checkboxes

This issue was resolved by creating custom jquery validation that if any of the other 3 checkboxes were on, the rest of the checkboxes were deselected.

Richard
  • 122
  • 12
0

The basic HTML for a radio button group is, e.g.

  <input type="radio" name="gender" value="male"> Male<br>
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="radio" name="gender" value="other"> Other
  • We need the name to be the same so that they behave together as a group
  • We need to pass in the value from the model so it displays correctly

You can get close with something like:

@Html.RadioButton("JobStatus", @Model.JobPostStatus.IsCorporate, new { @id = "JobPostStatus.IsCorporate" }) Corprate<br />
@Html.RadioButton("JobStatus", @Model.JobPostStatus.IsAllStores, new { @id = "JobPostStatus.IsAllStores" }) All<br />
@Html.RadioButton("JobStatus", "false") Select<br />

Which passes these requirements, but you are trying to bind a single HTML construct (radio button group) to multiple model properties. You will hit issues if you are intending to post this back, in which case you are going to be better off with your code and build the psuedo radio button group behavior in with JavaScript.

NikolaiDante
  • 18,469
  • 14
  • 77
  • 117
  • I can't change the name of the other 2 without losing the properties on a form submit – Richard Jan 25 '16 at 17:09
  • I more ment change the name of the static one, that you have control of the id of. – NikolaiDante Jan 25 '16 at 17:11
  • What would I do to get the other two to match up then, so all 3 can be one list? – Richard Jan 25 '16 at 17:13
  • What Id / Name do the other two get generated at when you inspect the HTML? – NikolaiDante Jan 25 '16 at 17:16
  • Names = JobPostStatus.IsAllStores, JobPostStatus.IsCorporate Ids=JobPostStatus_IsCorporate, JobPostStatus_IsAllStores – Richard Jan 25 '16 at 17:19
  • Im surprised that works as a RadioButton group for those two values. It doesn't in a [fiddle](https://jsfiddle.net/6Lf23vL3/) – NikolaiDante Jan 25 '16 at 17:25
  • It doesn't, that's what I'm trying to figure out is how to get them as a group without breaking what Razor uses on the post back to the controller, if you know how to word that better, feel free to edit the question! – Richard Jan 25 '16 at 17:27