c# - 创建仅将选中的单选按钮作为参数传递的方法调用
问题描述
逻辑是:我在视图上有几组单选按钮,它们负责一家公司的几个评级参数。例如:
<td>
<fieldset class="rating">
<input type="radio" id="param1" name="srating" value="5"/>
<input type="radio" id="param1" name="srating" value="4"/>
<input type="radio" id="param1" name="srating" value="3"/>
<input type="radio" id="param1" name="srating" value="2"/>
<input type="radio" id="param1" name="srating" value="1"/>
</fieldset>
</td>
<td>
<fieldset class="rating">
<input type="radio" id="param2" name="pgrating" value="5"/>
<input type="radio" id="param2" name="pgrating" value="4"/>
<input type="radio" id="param2" name="pgrating" value="3"/>
<input type="radio" id="param2" name="pgrating" value="2"/>
<input type="radio" id="param2" name="pgrating" value="1"/>
</fieldset>
</td>
然后我想将检查的参数传递给看起来像这样的控制器方法
public void SaveRating(int CompanyId, int param1, int param2)
{
// ...
}
那么问题来了:如何将所有检查的参数组合到一个方法调用中?我想使用 jquery 或 mvc 功能。也在寻找最有效的方法。
更新 它可以成为一个解决方案吗?
@using (Html.BeginForm("SaveRating", "MyController"))
{
<td>
<input style="display:none" name="CompanyID" value="@Model.ID"/>
<fieldset class="rating">
<input type="radio" id="sstar5" name="param1" value="5"/><label class="full" for="sstar5"></label>
<input type="radio" id="sstar4" name="param1" value="4"/><label class="full" for="sstar4"></label>
<input type="radio" id="sstar3" name="param1" value="3"/><label class="full" for="sstar3"></label>
<input type="radio" id="sstar2" name="param1" value="2"/><label class="full" for="sstar2"></label>
<input type="radio" id="sstar1" name="param1" value="1"/><label class="full" for="sstar1"></label>
</fieldset>
</td>
<td>
<fieldset class="rating">
<input type="radio" id="pgstar5" name="param2" value="5"/><label class="full" for="pgstar5"></label>
<input type="radio" id="pgstar4" name="param2" value="4"/><label class="full" for="pgstar4"></label>
<input type="radio" id="pgstar3" name="param2" value="3"/><label class="full" for="pgstar3"></label>
<input type="radio" id="pgstar2" name="param2" value="2"/><label class="full" for="pgstar2"></label>
<input type="radio" id="pgstar1" name="param2" value="1"/><label class="full" for="pgstar1"></label>
</fieldset>
</td>
<input type="submit" value="Save" />
}
解决方案
我使用 GET 方法为您的要求创建了一个示例(它有效)。
使用 AJAX 获取 URL/company/SaveRating?CompanyId=1¶m1=3¶m2=4
并使用 jquery 方法 addOrUpdateQueryStringParameter() 构建 URL 参数
<td>
<fieldset class="rating">
<input type="radio" id="param1" name="srating" value="5"/>
<input type="radio" id="param1" name="srating" value="4"/>
<input type="radio" id="param1" name="srating" value="3"/>
<input type="radio" id="param1" name="srating" value="2"/>
<input type="radio" id="param1" name="srating" value="1"/>
</fieldset>
</td>
<td>
<fieldset class="rating">
<input type="radio" id="param2" name="pgrating" value="5"/>
<input type="radio" id="param2" name="pgrating" value="4"/>
<input type="radio" id="param2" name="pgrating" value="3"/>
<input type="radio" id="param2" name="pgrating" value="2"/>
<input type="radio" id="param2" name="pgrating" value="1"/>
</fieldset>
</td>
<input type="button" value="rate" onclick="rate();"/>
<script>
function addOrUpdateQueryStringParameter(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
}
else {
return uri + separator + key + "=" + value;
}
}
function rate() {
var url = '/Company/SaveRating';
url = addOrUpdateQueryStringParameter(url, 'CompanyId', 1);
url = addOrUpdateQueryStringParameter(url, 'param1', $('input[name="srating"]:checked').val());
url = addOrUpdateQueryStringParameter(url, 'param2', $('input[name="pgrating"]:checked').val());
$.ajax({
url: url,
type: 'GET',
success: function (html) {
//todo
},
error: function (error) {
// handle
}
});
}
</script>
更新:
input type hidden
使用您的更新 cshtml 文件,如果您设置而不是,它运行良好(我刚刚测试过)style="display:none"
<input type="hidden" name="CompanyID" value="@Model.ID"/>
推荐阅读
- django - 网站付款:从客户到网站,反之亦然
- mongodb - 带有搜索的 MongoDB 排行榜查询
- reactjs - 如何避免一次又一次地调用 setState 函数?
- android - Flutter MVP错误处理手势时抛出以下NoSuchMethodError
- amazon-web-services - 在 AWS CloudFormation 中使用动态引用
- ruby - 从(可能是多行)字符串中提取第一行
- javascript - 如何解决“元素类型无效”错误?
- php - 使用 Woocommerce 产品创建下拉菜单并根据选择自动将产品添加到购物车
- api - 快照为空颤动
- notifications - 我如何向我所有的松弛用户发送消息