2017-04-10 103 views
1

我正在開發一個MVC項目,我有任務在複選框中選擇多個項目,並且希望將這些選定的項目保存到數據庫中。爲此,我創建了三個表格。第一個是DistrictMaster如何將多個選定的複選框值保存到數據庫mvc

public partial class DistrictMaster 
{ 
    public int Id { get; set; } 
    public string DistrictName { get; set; } 
    public virtual ICollection<PostMaster> PostMasters { get; set; } 
} 

這裏蔭進入各區,這樣可以節省數據庫 第二表郵政局長

public partial class PostMaster 
{ 
    public int Id { get; set; } 
    public string PostName { get; set; } 
    public string PIN { get; set; } 
    public Nullable<int> DistrictID { get; set; } 

    public virtual DistrictMaster DistrictMaster { get; set; } 
} 

這裏我根據從下拉列表中選擇的區進入不同的崗位。該DistrictID是外鍵,將pin碼和名稱保存到相應的區表

第三表AgentPostMapping

public partial class AgentPostMapping 
{ 
    public int Id { get; set; } 
    public Nullable<int> AgentId { get; set; } 
    public Nullable<int> PostId { get; set; } 
    public Nullable<System.DateTime> EffectDate { get; set; } 
} 

這裏的帖子ID不是外鍵,我需要保存選定或託運複選框中的項目作爲此postId的整數。這是Iam麻煩的一部分。

控制器

public ActionResult Create() 
    { 
     ViewBag.AgentId = new SelectList(db.AgentMasters, "ID", "FirstName"); 
     ViewBag.DistrictId = new SelectList(db.DistrictMasters, "Id", "DistrictName"); 
     return View(); 
    } 

public JsonResult GetPosts(string id) 
    { 
     List<SelectListItem> posts = new List<SelectListItem>(); 
     var postList = this.Getpost(Convert.ToInt32(id)); 
     ViewBag.Postdata= this.Getpost(Convert.ToInt32(id)); 
     var postData = postList.Select(m => new SelectListItem() 
     { 
      Text = m.PostName, 
      Value = m.Id.ToString(), 
     }); 
     return Json(postData, JsonRequestBehavior.AllowGet); 
    } 

public IList<PostMaster> Getpost(int DistrictId) 
    { 
     return db.PostMasters.Where(m => m.DistrictID == DistrictId).ToList(); 
    } 

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Create([Bind(Include = "Id,AgentId,PostId,EffectDate")] AgentPostMapping agentPostMapping, FormCollection formdata) 
    { 
     if (ModelState.IsValid) 
     { 
      db.AgentPostMappings.Add(agentPostMapping); 
      await db.SaveChangesAsync(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.AgentId = new SelectList(db.AgentMasters, "ID", "FirstName", agentPostMapping.AgentId); 
     return View(agentPostMapping); 
    } 

這是我的控制器的一部分,我沒有寫在[HttpPost]代碼。任何人都可以請幫我寫代碼保存到分貝。

視圖(創建)

@model BeyondThemes.BeyondAdmin.Models.AgentPostMapping 

@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
@Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(model => model.AgentId,"AgentName", htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownList("AgentId", null, "select", htmlAttributes: new { @class = "form-control" }) 
      @Html.ValidationMessageFor(model => model.AgentId, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

<div class="form-group"> 
     @Html.Label("District", htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownList("DistrictId", ViewBag.DistrictId as SelectList, "-- Please Select --", new { style = "width:250px" }) 
      @*@Html.DropDownList("DistrictID", null, "select", htmlAttributes: new { @class = "form-control" })*@ 
      @*@Html.ValidationMessageFor(model => model.PostMaster.DistrictID, "", new { @class = "text-danger" })*@ 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.PostId,"PostName", htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      <div id="PostMaster"></div> 
      @Html.ValidationMessageFor(model => model.PostId, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Create" class="btn btn-default" /> 
     </div> 
    </div> 
</div> 

} 

<script type="text/javascript"> 
$(document).ready(function() { 
//District Dropdown Selectedchange event 
$("#DistrictId").change(function() { 
    $("#PostMaster").empty(); 
    $.ajax({ 
     type: 'POST', 
     url: '@Url.Action("GetPosts")', // Calling json method 
     dataType: 'json', 
     data: { id: $("#DistrictId").val() }, 
     // Get Selected post id. 
     success: function (posts) { 
      $.each(posts, function (i, post) { 
       //$("#PostMaster").append('<option value="' + post.Value + '">' + 
       // post.Text + '</option>'); 

       $("#PostMaster").append('<input type="checkbox" name="postdata">' + post.Text+'<br>'); 
      }); 
     }, 
     error: function (ex) { 
      alert('Failed to retrieve post.' + ex); 
     } 
    }); 
    return false; 
}) 
}); 

這是頁面怎麼看都像

enter image description here

當我打的創建按鈕通過選擇屏幕截圖複選框選中的複選框未在[HttpPost]中創建參數

它這樣表示

enter image description here

這是爲什麼在沒有帖子ID顯示的值。任何人都可以請幫我找到一個解決方案。我該如何解決這個問題?

+0

您的複選框的模式應該是一個字符串數組。每個選定的複選框都與數組中的一個字符串元素(它是ID)相對應。如果你只用複選框創建一個簡單的演示,也許這個問題更容易回答。請在您的問題中顯示查看。 – roberth

+0

我在我的問題中包含了查看頁面 – user355

+0

它對我來說看起來很複雜,但也許我錯過了你想要做的事情。看看這個解決方案:http://stackoverflow.com/questions/25038736/post-checked-checkboxes-to-controller-action-without-using-html-helper-like-html – roberth

回答

0

每一件事情似乎是好了你的問題的解決方案 就是可以使用name屬性獲取元素的值。 要麼改變name屬性NAME = 「POSTDATA」NAME = 「帖子ID」在jQuery的你在哪裏試圖追加複選框。

,或者你可以在模型更改屬性名稱到帖子ID postdta

0

我想你應該創建一個名爲'PostId'的input

我看到一個@Html.LabelFor@Html.ValidationMessageFor,但EditorFor在哪裏?

你並不需要使用HTML的助手,地方需要有一個input(複選框),名稱爲「PostId

+0

Iam在jquey中獲取選中的複選框項目,但是如何將此值傳遞給控制器​​以將這些選定項目保存到數據庫 – user355

相關問題