我有一个 jQuery getJson 方法,它正确地返回一个选定的 CustomerID。发生这种情况时,将启用一个按钮以将 CustomerID 发送到另一个控制器方法,然后该方法将打开一个表单来修改客户。
我检查了表单集合,但没有与集合 ID 关联的值。它只是以“CustomerID”的形式出现,没有任何价值。
如何使用表单集合成功将此值传递给另一个控制器?
我知道脚本部分包含 var CustomerID 并且 BeginForm 具有隐藏字段值,但显然,两者之间没有“联系”。我想知道我怎么能做到这一点......
所以基本上,我怎样才能将 CustomerID 中的 JS 变量值放入表单中?
JS
@section scripts {
<script type="text/javascript">
$(function ()
{
var selID = null;
var CustomerID = null;
$("#Name").change(function ()
{
selID = $("#Name option:selected").val();
var url = '/Project/SpecificCustomer';
var param = { Id: selID };
$.getJSON(url, param, function (data)
{
var html = "<table border='1' cellpadding='3'>";
html += "<tr>";
html += "<td>" + "Customer ID: " + data.CustomerID + "</td>";
CustomerID = data.CustomerID;
html += "</tr>";
html += "<tr>";
html += "<td>" + "Email: " + data.Email + "</td>";
html += "</tr>";
html += "<tr>";
var FirstName = data.FirstName;
FirstName == null ? "" : FirstName;
var LastName = data.LastName;
LastName == null ? "" : LastName;
html += "<td>" + "Name: " + FirstName + " " + LastName + "</td>";
html += "</tr>";
html += "<tr>";
var date1 = new Date(parseInt(data.CreatedDate.substr(6)));
date1 == null ? "" : date1;
html += "<td>" + "Created: " + date1 + "</td>";
html += "</tr>";
html += "<tr>";
var date2 = new Date(parseInt(data.UpdatedDate.substr(6)));
date2 == null ? "" : date2;
html += "<td>" + "Updated: " + date2 + "</td>";
html += "</tr>";
html += "</table>";
$("#divData").html('');
$("#divData").append(html);
if (CustomerID != null)
$("#UpdateCust").prop('disabled', false);
});
});
});
</script>
}
@using (Html.BeginForm("Index", "Customer", new AjaxOptions { HttpMethod = "POST" }))
{
<fieldset>
<legend>Edit Customer</legend>
<div class="editor-field">
<input type="hidden" id="custID" name="custID" value="CustomerID" />
<input type="submit" id="UpdateCust" value='@Resources.Update' disabled="disabled" />
</div>
</fieldset>
}
其他控制器方法
public ActionResult Index(FormCollection form)
{
string custID = form["custID"];
...