我正在尝试在控制器中的 httppost 事件上显示 jQuery 模式弹出窗口,例如 Web 表单中的 RegisterClientScript。在 _layout.cshtml 我有这个:
<script>
$(function () {
$("#dialog-modal").dialog({
autoOpen: false,
height: 140,
modal: true
});
});
</script>
在我看来,我有这个:
@{
ViewBag.Title = "Add New Record";
}
<h2>Add New Record</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<p>Agents @Html.DropDownList("Agents", String.Empty) <input type="submit" name="Get Agent" value="Get Agent" title="Get Agent" id="btnGetAgent" /></p>
<legend>Entries</legend>
<div class="editor-label">
First Name
</div>
<div class="editor-field">
@Html.TextBox("FirstName", (string)ViewBag.FirstName)
</div>
<div class="editor-label">
Last Name
</div>
<div class="editor-field">
@Html.TextBox("LastName", (string)ViewBag.LastName)
</div>
<div class="editor-label">
Address
</div>
<div class="editor-field">
@Html.TextBox("Address", (string)ViewBag.Address)
</div>
<div class="editor-label">
City/State/Zip
</div>
<div class="editor-field">
@Html.TextBox("City", (string)ViewBag.City, new { style="width:180px" }) @Html.TextBox("Zip", (string)ViewBag.Zip, new { style="width:60px" })
</div>
<div class="editor-label">
Business#
</div>
<div class="editor-field">
@Html.TextBox("Business", (string)ViewBag.Business)
</div>
<div class="editor-label">
Mobile#
</div>
<div class="editor-field">
@Html.TextBox("Mobile", (string)ViewBag.Mobile)
</div>
<div class="editor-label">
Fax#
</div>
<div class="editor-field">
@Html.TextBox("Fax", (string)ViewBag.Fax)
</div>
<div class="editor-label">
Email
</div>
<div class="editor-field">
@Html.TextBox("Email", (string)ViewBag.Email)
</div>
<div class="editor-label">
Position
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<div id="dialog-modal" title="Confirmation">
<p>ViewBag.Confirmation</p>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
The dialog-modal div should be displayed on postback in jquery modal window along with the data.
控制器:
[InitializeSimpleMembership]
[HttpPost]
public ActionResult AddNewRecord(int? AgentID, FormCollection collection)
{
if (collection["FirstName"].ToString() != string.Empty)
{
Agents agent = new Agents();
agent.A_FirstName = collection["FirstName"].ToString();
agent.A_LastName = collection["LastName"].ToString();
agent.A_Address = collection["Address"].ToString();
agent.A_City = collection["City"].ToString();
agent.A_State = collection["State"].ToString();
agent.A_ZipCode = collection["Zip"].ToString();
agent.A_Phone = collection["Business"].ToString();
agent.A_FAX = collection["Fax"].ToString();
agent.Email = collection["Email"].ToString();
_fe.Agents.Add(agent);
_fe.SaveChanges();
string username = agent.A_FirstName + agent.A_LastName;
string password = GenerateRandomPassword(8);
WebSecurity.CreateUserAndAccount(username, password);
ViewBag.Confirmation = "Record Entered";
AppHelper.SendWelcomeEmail("", "", username, password);
AppHelper.SendWelcomeEmailAdminNotification("", "", agent.A_FirstName, agent.A_LastName, true, agent.Email, agent.AgentID, username);
}
var modelAgent = _fe.Agents.Take(10);
SelectList sl = new SelectList((from s in _aa.Agents.ToList() select new { COUNTER = s.COUNTER, FullName = s.LASTNAME + ", " + s.FIRSTNAME }), "COUNTER", "FullName", null);
ViewBag.Agents = sl;
if (AgentID != null && AgentID != 0)
{
var modelAgentFilter = from s in _aa.Agents
where s.AgentID == AgentID
select s;
if (modelAgentFilter != null)
{
foreach (var property in modelAgentFilter)
{
ViewBag.FirstName = property.FIRSTNAME;
ViewBag.LastName = property.LASTNAME;
ViewBag.Address = property.ADDRESS;
ViewBag.State = property.STATE;
ViewBag.City = property.CITY;
ViewBag.Zip = property.ZIPCODE;
ViewBag.Business = property.BUSPHONE;
ViewBag.Mobile = property.HOMEPHONE;
ViewBag.Fax = property.FAXPHONE;
ViewBag.Email = property.Email;
}
}
return View(modelAgentFilter);
}
return View();
}
现在您可能会看到一些 webforms 语法,但我正在从 web forms 转向 MVC。正如您所看到的,当我返回模型时,我还想包含一些代码来触发显示模式弹出窗口。
哪种方法最好?