1

有人可以帮我解决我的问题吗?我搜索了我可以使用 AJAX,但我不知道如何...当我单击 clientLocationSave 上的保存按钮时,如果有数据,我还可以保存联系表单中的字段...

<div id="contentMain">
    @using (Html.BeginForm("ClientLocationSave", "Client", FormMethod.Post, new { id = "clientLocForm" }))
    {
    <input type="hidden" id="clientId" name="clientId" value="@ViewBag.ClientId" />
    <input type="hidden" id="clientLocId" name="clientLocId" value="@clientLocId" /> 

    <h2>
        Client Location @pageAction</h2>
    <div class="main">
        <p>
            <label for="txtName">
                Name</label>
            <span>
                <input type="text" id="txtName" name="txtName" class="validate[required] inputLong" value="@clientLocName" />
            </span>
        </p>
        <p>
            <label for="txtAddress1">
                Address 1</label>
            <span>
                <input type="text" id="txtAddress1" name="txtAddress1" class="validate[required] inputLong" value="@addressLine1" />
            </span>
        </p>
        <p>
            <label for="txtAddress2">
                Address 2</label>
            <span>
                <input type="text" id="txtAddress2" name="txtAddress2" class="inputLong" value="@addressLine2" />
            </span>
        </p>
        <p>
            <label for="txtCity">
                City</label>
            <span>
                <input type="text" id="txtCity" name="txtCity" class="validate[required] inputLong" value="@city" />
            </span>
        </p>
        <p>
            <label for="ddlState">
                State</label>
            <span>
                @Html.DropDownList("ddlState", new SelectList(ViewBag.StateList, "ID", "Display_Value", state), "[Please Select]",
            new Dictionary<string, object>
            {
                {"class","validate[required] inputLong"}
            })
            </span>
        </p>
        <p>
            <label for="txtZipCode">
                Zip Code</label>
            <span>
                <input type="text" id="txtZipCode" name="txtZipCode" class="validate[required,custom[onlyNumberSp],maxSize[20]] inputLong" value="@zipCode" />
            </span>
        </p>
    </div>
    <input type="submit" id="btnSave" class="styledButton" value="Save" />

}

<div class="main">
     @using (Html.BeginForm("ClientLocationContactSave", "Client", FormMethod.Post, new { id = "contactForm" }))
    {
        <input type="hidden" id="clientId" name="clientId" value="@clientId" />
        <input type="hidden" id="clientLoctContactId" name="clientLoctContactId" value="@clientLoctContactId" />
        <input type="hidden" id="clienLocatId" name="clienLocatId" value="@clientLocId" />

        <p>
            <label for="ddlContact">
                Contact Type</label>
            <span>
                @Html.DropDownList("ddlContact", new SelectList(ViewBag.ContactType, "ID", "Display_Value", contactTypeLookId), "[Please Select]",
            new Dictionary<string, object>
            {
                {"class","validate[required] inputLong"}
            })
            </span>
        </p>
        <p>
            <label for="txtValue">
                Contact Value</label>
            <span>
                <input type="text" id="txtValue" name="txtValue" class="validate[required] inputLong"
                    value="" />
            </span>
        </p>
        <p>
            <label for="chkSaveIsPrimary">
                Is Primary</label>
            <input type="checkbox" name="chkSaveIsPrimary" id="chkSaveIsPrimary" value="true"
                checked="checked" />
        </p> 
controller:
[HttpPost]
    public ActionResult ClientLocationSave(FormCollection formCollection)
    {
        String msg = String.Empty;
        String newClientLocationId = String.Empty;
        String clientId = formCollection["clientId"];
        String clientLocId = formCollection["clientLocId"];
        String locationName = formCollection["txtName"];
        String address1 = formCollection["txtAddress1"];
        String address2 = formCollection["txtAddress2"];
        String city = formCollection["txtCity"];
        String state = formCollection["ddlState"];
        String zipCode = formCollection["txtZipCode"];

        Client_Location clientLoc = new Client_Location();
        try
        {
            if (String.IsNullOrWhiteSpace(clientLocId) || clientLocId == "0")
            {
                clientLoc.ClientID = Convert.ToInt32(clientId);
                clientLoc.Name = locationName.Trim();
                clientLoc.Address_Line1 = address1;
                clientLoc.Address_Line2 = address2;
                clientLoc.City = city;
                clientLoc.State_LookID = Convert.ToInt32(state);
                clientLoc.ZipCode = zipCode;
                clientLoc.DateCreated = DateTime.UtcNow;
                clientLoc.DateModified = DateTime.UtcNow;
                clientLoc.CreatedBy = User.Identity.Name;
                clientLoc.ModifiedBy = User.Identity.Name;

                db.Client_Location.Add(clientLoc);
            }
            else
            {
                int id = Convert.ToInt32(clientLocId);
                clientLoc = (from a in db.Client_Location
                             where a.ID == id
                             select a).SingleOrDefault();

                clientLoc.Name = locationName.Trim();
                clientLoc.Address_Line1 = address1;
                clientLoc.Address_Line2 = address2;
                clientLoc.City = city;
                clientLoc.State_LookID = Convert.ToInt32(state);
                clientLoc.ZipCode = zipCode;
                clientLoc.DateModified = DateTime.UtcNow;
                clientLoc.ModifiedBy = User.Identity.Name;
            }
        }
        catch (Exception)
        {
            msg = "Failed to save";
        }



        db.SaveChanges();
        if (String.IsNullOrWhiteSpace((msg)))
        { TempData["message"] = "Client Location Saved Successfully."; }
        else if (msg != "")
        { TempData["message"] = msg; }

        newClientLocationId = clientLoc.ID.ToString();

        return RedirectToAction("ClientLocationDetails", new { clientId = clientId, clientLocId = newClientLocationId });

    }
[HttpPost]
    public ActionResult ClientLocationContactSave(FormCollection formCollection)
    {
        String msg = String.Empty;
        String clientId = formCollection["clientId"];
        String clientLoctContactId = formCollection["clientLoctContactId"];
        String clienLocatId = formCollection["clienLocatId"];
        bool isPrimary = Convert.ToBoolean(formCollection["chkSaveIsPrimary"]);
        String value = formCollection["txtValue"];
        String contactTypeLookId = formCollection["ddlContact"];


        Client_Location_Contact clientLoc = new Client_Location_Contact();
        try
        {
            if (String.IsNullOrWhiteSpace(clientLoctContactId) || clientLoctContactId == "0")
            {
                clientLoc.Client_LocationID = Convert.ToInt32(clienLocatId);
                clientLoc.Value = value.Trim();
                clientLoc.IsPrimary = isPrimary;
                clientLoc.ContactType_LookID = Convert.ToInt32(contactTypeLookId);
                clientLoc.DateCreated = DateTime.UtcNow;
                clientLoc.DateModified = DateTime.UtcNow;
                clientLoc.CreatedBy = User.Identity.Name;
                clientLoc.ModifiedBy = User.Identity.Name;

                db.Client_Location_Contact.Add(clientLoc);
            }
            else
            {
                int id = Convert.ToInt32(clientLoctContactId);
                clientLoc = (from a in db.Client_Location_Contact
                             where a.ID == id
                             select a).SingleOrDefault();

                clientLoc.Value = value.Trim();
                clientLoc.IsPrimary = isPrimary;
                clientLoc.ContactType_LookID = Convert.ToInt32(contactTypeLookId);
                clientLoc.DateModified = DateTime.UtcNow;
                clientLoc.ModifiedBy = User.Identity.Name;
            }
        }
        catch (Exception)
        {
            msg = "Failed to save";
        }


        db.SaveChanges();
        if (String.IsNullOrWhiteSpace((msg)))
        { TempData["message"] = "Contact Saved Successfully."; }
        else if (msg != "")
        { TempData["message"] = msg; }


        ViewBag.clientLoctContactId = clientLoctContactId;
        ViewBag.clienLocatId = clienLocatId;
        return RedirectToAction("ClientLocationDetails", new { clientLocId = clienLocatId, clientId = clientId });
    }
4

1 回答 1

0

您可以在 Jquery 中使用 serialize() 函数并使用 Ajax 发布数据。

   $("#submit_btn_id").click(function(){
    var values = $("#frm_id1").serialize(); 
    values += "&"+$("#frm_id2").serialize(); 

    $.ajax({
                type : "POST",
                url : "url_to_post_data_to",
                data : values,                  
                success: function(data) {
                    //do something on success
                }

           });
  });

url to post 是您要发布数据的路径。

于 2013-07-25T04:06:51.093 回答