Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ClientModelData
- {
- public int ClientID { get; set; }
- public string ClientName { get; set; }
- public string Phone { get; set; }
- public string Address { get; set; }
- public IList<int> LocationID { get; set; }
- }
- [HttpPost]
- public ActionResult AddClient(ClientModelData client)
- {
- Client _client = new Client()
- {
- //ClientCode=client.ClientCode,
- ClientName = client.ClientName,
- Phone = client.Phone,
- Ext = client.Ext,
- Address = client.Address,
- CreatedBy = 1,
- CreatedDate = DateTime.Now,
- ModifiedBy = 1,
- ModifiedDate = DateTime.Now,
- ModifiedIP = Request.ServerVariables["REMOTE_ADDR"]
- };
- db.Clients.Add(_client);
- try
- {
- db.SaveChanges();
- }
- catch (DbEntityValidationException dbEx)
- {
- string error = string.Empty;
- foreach (var validationErrors in dbEx.EntityValidationErrors)
- foreach (var validationError in validationErrors.ValidationErrors)
- error += string.Format("Property: {0} Error: {1}", validationError.PropertyName,
- validationError.ErrorMessage);
- return Json(new
- {
- status = false,
- message = "Unable to update information for selected client, please contact administrator. For detail <a class='errordetail'>click here</a>",
- exception = error
- });
- }
- return Json(_client);
- }
- <ul class="locations">
- @foreach (var item in Model.Locations)
- {
- <li>
- <label>
- <input type="checkbox" id="chkLocations" name="chkLocations"
- value="@item.LocationID" />
- @item.LocationTitle</label>
- </li>
- }
- </ul>
- var allVals = [];
- $('#chkLocations:checked').each(function () {
- allVals.push($(this).val());
- });
- var dataPacket = {
- ClientName: $("#txtClientName").val(),
- Phone: $("#txtPhone").val(),
- Ext: $("#txtExt").val(),
- Address: $("#txtAddress").val(),
- LocationID: allVals
- };
- var allVals = [];
- $('#chkLocations:checked').each(function () {
- allVals.push($(this).val());
- });
- var dataPacket = {
- ClientName: $("#txtClientName").val(),
- Phone: $("#txtPhone").val(),
- Ext: $("#txtExt").val(),
- Address: $("#txtAddress").val(),
- LocationID: allVals
- };
- $.ajax({
- url: '@Url.Action("AddClient", "SomeController")',
- type: 'POST',
- contentType: 'application/json',
- data: JSON.stringify({ client: dataPacket }),
- success: function(result) {
- // ... handle the result
- }
- });
Add Comment
Please, Sign In to add comment