mvc razor从控制器调用jquery模式弹出脚本

时间:2013-10-03 22:16:15

标签: jquery asp.net-mvc jquery-ui asp.net-mvc-4 razor

我试图在控制器中的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)&nbsp;&nbsp; <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表单转移到mvc,请原谅我的方法。正如您在返回模型时所看到的,我还想包含一些代码来触发显示模式弹出窗口。 请告诉我哪种方法最好。 非常感谢Laziale

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解正确。如果成功创建了新用户,则会收到AgentID。所以你可以试试这样的东西:

   //Delete "autoOpen: false" option from your dialog     

   @if(Model.AgentID != null)
     {
       <script>
            $(function () {
                $("#dialog-modal").dialog({
                    height: 140,
                    modal: true
                });
            });
        </script> 
    }