下面的代码有效,但我无法区分一种方法来检索正确的隐藏字段以传递正确的单选按钮值。有多个具有相同名称的隐藏字段,jQuery将获取第一个radioAccessId。
关于我如何获得每个listaccesshold span的指定隐藏字段的任何建议?
<div class="row">
<div class="col-xs-12">
<ul class="list-unstyled margin-top-10 margin-bottom-10">
@For Each access As AccessLevel In Model.AssignedAccessLevels
If access.AccessLevelId <> 0 Then
@
<span class="listaccessholder-@access.AccessLevelId">
<li>
<div class="input-group">
@access.AccessLevelName
<div class="icheck-inline">
@code
If access.AccessFlag = "Yes" Then
@<label>
<input checked type="radio" name="AccessFlag-@access.AccessLevelId" value="Yes" group="AccessFlag-@access.AccessLevelId"> Yes
</label>
@<label>
<input type="radio" name="AccessFlag-@access.AccessLevelId" value="No" group="AccessFlag-@access.AccessLevelId"> No
</label>
Else
@<label>
<input type="radio" name="AccessFlag-@access.AccessLevelId" value="Yes" group="AccessFlag-@access.AccessLevelId"> Yes
</label>
@<label>
<input checked type="radio" name="AccessFlag-@access.AccessLevelId" value="No" group="AccessFlag-@access.AccessLevelId"> No
</label>
End If
End Code
</div>
<input type="hidden" id="radioAccessId" value="@access.AccessLevelId" />
</div>
</li>
</span>
End If
Next
</ul>
</div>
</div>
Razor会生成:
<div class="row">
<div class="col-xs-12">
<ul class="list-unstyled margin-top-10 margin-bottom-10">
<span class="listaccessholder-353">
<li>
<div class="input-group">
Epi
<div class="icheck-inline">
<label>
<input type="radio" name="AccessFlag-353" value="Yes" group="AccessFlag-353"> Yes
</label>
<label>
<input checked type="radio" name="AccessFlag-353" value="No" group="AccessFlag-353"> No
</label>
</div>
<input type="hidden" id="radioAccessId" value="353" />
</div>
</li>
</span>
<span class="listaccessholder-388">
<li>
<div class="input-group">
System Administrator
<div class="icheck-inline">
<label>
<input checked type="radio" name="AccessFlag-388" value="Yes" group="AccessFlag-388"> Yes
</label>
<label>
<input type="radio" name="AccessFlag-388" value="No" group="AccessFlag-388"> No
</label>
</div>
<input type="hidden" id="radioAccessId" value="388" />
</div>
</li>
</span>
<span class="listaccessholder-392">
<li>
<div class="input-group">
Site Administrator
<div class="icheck-inline">
<label>
<input type="radio" name="AccessFlag-392" value="Yes" group="AccessFlag-392"> Yes
</label>
<label>
<input checked type="radio" name="AccessFlag-392" value="No" group="AccessFlag-392"> No
</label>
</div>
<input type="hidden" id="radioAccessId" value="392" />
</div>
</li>
</span>
<input type="hidden" id="radioviewId" value="6" />
</ul>
</div>
</div>
jQuery 我的目标是获取带有YES / NO的部分并传递值,但是我无法获得没有accessId的部分。每个accessId都保存在一个隐藏字段中并且具有相同的名称。
$(function () {
// Someone has clicked one of the access radio buttons
var accessId = $(this).find('#radioAccessId').val();
var listaccessholder = 'span.listaccessholder-' + accessId;
var radioViewId = $(this).find('#viewId').val();
$(listaccessholder).click(function () {
var accessFlag = "";
$(this).find('input[type=radio]:checked').each(function () {
accessFlag = $(this).val();
});
// Make a viewModel instance
var viewModel = new Object();
viewModel.viewId = radioViewId;
viewModel.AccessId = accessId;
viewModel.AccessFlag = accessFlag;
//Ajax call to post the viewModel to the controller
var strung = JSON.stringify(viewModel);
$.ajax({
url: app_base + 'Menu/UpdateAccess',
type: 'POST',
data: strung,
contentType: 'application/json; charset=utf-8',
success: function (data) {
ShowUserMessage("Access updated");
},
error: function (xhr, ajaxOptions, thrownError) {
ShowUserMessage("Error: " + xhr.status + " " + thrownError);
}
});
})
});
我想继续采用这种方法,但我需要一些关于SOF社区的建议。
控制器
<HttpPost>
Public Sub UpdateAccess(viewModel As AjaxAccessViewModel)
If Request.IsAjaxRequest() Then
Dim status As New Boolean
Try
status = _menuService.UpdateAccessFlag(viewModel.ViewId, viewModel.AccessId, viewModel.AccessFlag, GetUserInfo.UserID)
Catch ex As Exception
'TempData("message") = New GenericMessage() With
' {.Message = "Update failed.",
' .MessageType = GenericMessages.danger}
End Try
If (status = True) Then
'TempData("message") = New GenericMessage() With
' {.Message = "Successfully updated the access flag.",
' .MessageType = GenericMessages.success}
End If
End If
End Sub