2017-04-10 63 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

相关问题