我是MVC的新手。我有2个表,即tb_RepDetails和tb_AgentDetails。在View中,用户可以选择"代表"或者"代理"通过单选按钮,因此根据用户选择," RepId"或者" AgentId"将在下拉列表上绑定。我想传递所选的" Id" (VARCHAR)作为存储过程中的参数。
<tr>
<td><b>Mediator</b> </td>
<td>:</td>
<td>
<input type="radio" name="Mediator" id="Rep1" value="Representative" style="width:5%" checked="checked" />REPRESENTATIVE
<input type="radio" name="Mediator" id="Agent1" value="Agent" style="width:5%" />AGENT
</td>
</tr>
<tr>
<td><b>Identificaton Number</b> </td>
<td>:</td>
<td>
@Html.DropDownList("EmpNo", new SelectList(ViewBag.Emp), "select", new { required = "required", style = "height:28px", onchange = "detailsempt()" })<b style="color: red;">*</b> <span id="IDddl"></span>
</td>
</tr>
<script>
function detailsempt() {
$.ajax({
url: '@Url.Action("GetEmpDetails", "Advertiser")',
type: 'post',
dataType: 'json',
data: { Usr: $("#EmpNo").val() },
async: false,
success: function (data) {
$("#MedDetails").val(data);
}
});
}
</script>
答案 0 :(得分:0)
根据Stepen Muecke和其他人的指导,我完成了这项任务。我给出了代码,以便它可以帮助其他人以供将来参考。欢迎任何人都可以这样做。
<tr>
<td><b>Payment Method</b> </td>
<td>:</td>
<td>
@foreach (var pay in ViewBag.RadioPayList)
{
<input type="radio" name="Payment" style="width:5%" value="@pay" required onclick="selectPayMode(this)" />@pay
}
</td>
</tr>
<tr>
<td><b>Mediator</b> </td>
<td>:</td>
<td>
@foreach (var i in ViewBag.RadioList)
{
<input type="radio" name="Mediator" style="width:5%" value="@i" required onclick="selectEmpid(this)" />@i
}
</td>
</tr>
JQuery的:
<script src="~/Scripts/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
//First time according the checked radio button to populate ddl
RadioLoadData();
$("input[name='Mediator']").change(function () {
//reload dropdownlist
RadioLoadData();
})
});
function RadioLoadData() {
$("input[name='Mediator']").each(function () {
if ($(this).is(":checked")) {
var value = $(this).val();
var ddlId = $("#ddlEmpNo");
$.ajax({
type: "Post",
url: '@Url.Action("GetEmpId", "Advertiser")',
data: '{value: "' + value + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
ddlId.html("");
//var selectvalue = "";
for (var i = 0; i < data.length; i++) {
ddlId.append($('<option></option>').val(data[i].Value).html(data[i].Text));
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to retrieve ID Numbers');
}
});
}
});
}
</script>
控制器:
public JsonResult GetEmpId(string value)
{
List<SelectListItem> items = new List<SelectListItem>();
string connstr1 = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection con = new SqlConnection(connstr1);
con.Open();
List<SelectListItem> list = new List<SelectListItem>();
if (value == "AGENT")
{
// query the tb_AgentDetails table and get values.
SqlCommand cmd1 = new SqlCommand("select AgentId from tb_AgentDetails", con);
using (SqlDataReader rdr = cmd1.ExecuteReader())
{
List<string> result = new List<string>();
while (rdr.Read())
{
string EmpId = rdr["AgentId"].ToString();
result.Add(EmpId);
}
rdr.Close();
for (int i = 0; i < result.Count; i++)
{
list.Add(new SelectListItem { Text = result[i].ToString(), Value = result[i].ToString() });
}
con.Close();
}
}
if (value == "REPRESENTATIVE")
{
//query the tb_RepDetails table and get values.
SqlCommand cmd1 = new SqlCommand("select RepId from tb_RepDetails", con);
using (SqlDataReader rdr = cmd1.ExecuteReader())
{
List<string> result = new List<string>();
while (rdr.Read())
{
string EmpId = rdr["RepId"].ToString();
result.Add(EmpId);
}
rdr.Close();
for (int i = 0; i < result.Count; i++)
{
list.Add(new SelectListItem { Text = result[i].ToString(), Value = result[i].ToString() });
}
con.Close();
}
}
return Json(list, JsonRequestBehavior.AllowGet);
}