我在javascript函数中使用html helper for dropdownlist但它不起作用。我尝试使用各种语法有或没有编码器等但它渲染为字符串而不是html控件。实际上,每次单击“添加新行”按钮时,我都必须创建动态下拉列表。在视图中放置下拉列表的代码工作正常但在javascript文件中它呈现为简单文本。
addServer.js-WAY1
function addServerRow() {
var html = '<tr>' +
'<td><input type="text" name="ServerAllNetworks.ServerPrivateNetworks[' + _totalRows + '].IPAddress" class="form-control" id="' + 'txtIpAddress' + _totalRows + '"></td>' +
'<td>@Ajax.JavaScriptStringEncode(Html.DropDownListFor(m => m.ServerPrivateNetworks, ViewData["PrivateNetwork"] as List<SelectListItem>, "Select",new { Class = "form-control", data_required = "true"}).ToHtmlString())</td>' +
'</tr>'
$(html).appendTo($("#tblServerPrivateNw"))
};
$("#btnAddPrivateServerNetwork").click(function (e) {
e.preventDefault();
_totalRows += 1;
addServerRow();
});
我已经尝试了另一种方法来创建dropdownlist它工作正常但是当我从动态创建的下拉列表中选择项目时,我在表单提交的控制器中获得null值。以下是使用的代码
addServer.js-Way2
function addServerRow() {
var html = '<tr>' +
'<td><input type="text" name="ServerAllNetworks.ServerPrivateNetworks[' + _totalRows + '].IPAddress" class="form-control" id="' + 'txtIpAddress' + _totalRows + '"></td>' +
'<td><select class="form-control" name="ServerAllNetworks.ServerPrivateNetworks" id="' + 'drpPrivateNetwork' + _totalRows + '" /></td>' +
'<td><input type="button" class="btnremove" value="-" /></td>' +
'</tr>'
$(html).appendTo($("#tblServerPrivateNw"))
var drplist = document.getElementById('drpPrivateNetwork' + _totalRows);
for (i = 0; i < allPrivateNetworks.length; i++) {
var option = document.createElement('option');
option.text = allPrivateNetworks[i].Name;
option.value = allPrivateNetworks.Id;
drplist.add(option, 0);
}
};
$("#btnAddPrivateServerNetwork").click(function (e) {
e.preventDefault();
_totalRows += 1;
addServerRow();
});
此下拉列表的模型是ServerAllNetworks,dropdownlist用于获取PrivateNetworkId
public class ServerAllNetworks
{
public ServerDetails server { get; set; }
public List<ServerPrivateNetworkDetails> ServerPrivateNetworks { get; set; }
}
public class ServerPrivateNetworkDetails : ServerPrivateNetwork
{
public string ServerName { get; set; }
public string PrivateNetworkName { get; set; }
}
public partial class ServerPrivateNetwork
{
public int ID { get; set; }
public int ServerID { get; set; }
public int PrivateNetworkID { get; set; }
public string IPAddress { get; set; }
}
我可以使用这两种方式中的任何一种,请建议哪种方式更好,然后以这种方式建议解决问题的方法。
我的控制器代码是
[HttpPost]
public ActionResult Add(FormCollection form, int ServerTypeID)
{
GetDropDownList();
ServerAllNetworks serverAllNetworks = new ServerAllNetworks();
ServerDetails serverDetails = new ServerDetails();
serverDetails.ServerName = Request["server.ServerName"].ToString();
serverDetails.ServerTypeID = Convert.ToInt16((Request["server.ServerTypeID"]).ToString());
serverDetails.Domain = Request["server.Domain"].ToString();
serverDetails.NickName = Request["server.NickName"].ToString();
serverDetails.ServiceProviderID = Convert.ToInt32(Request["server.ServiceProviderId"]);
serverDetails.PublicIP = Request["server.PublicIP"];
serverAllNetworks.server = serverDetails;
var privateNetworkIdsList = Request["\"ServerPrivateNetworks\""];
var newprivateNetworkIdsList = Regex.Replace(privateNetworkIdsList, "[^.0-9,]", "").Split(',').ToList();
for (int i = 1; i <= newprivateNetworkIdsList.Count; i++ )
{
ServerPrivateNetworkDetails serverPrivateNetworkDetails = new ServerPrivateNetworkDetails();
serverPrivateNetworkDetails.IPAddress = Request["ServerAllNetworks.ServerPrivateNetworks.IPAddress" + i].ToString();
serverPrivateNetworkDetails.PrivateNetworkID = Convert.ToInt32(newprivateNetworkIdsList[0]);
serverAllNetworks.ServerPrivateNetworks.Add(serverPrivateNetworkDetails);
}
//if (!ValidateServer(serverAllNetworks.server, ModelState))
//{
// var result = AdminService.AddServer(serverAllNetworks);
// ViewBag.Message = result > 0 ? Message.ServerAddSuccessfully : Message.ServerAddError;
// SessionItems.ServerTypes = GetMenuItem();
// if (result > 0)
// return RedirectToAction("Dashboard", "Home");
//}
return View();
}
目前我正在使用way1但是已将代码置于视图中。现在我面临的问题是servertypeid和serviceproviderid的下拉列表我得到的所有下拉值都不仅仅是所选的值。请帮忙。