【问题标题】:How to save data from Select box in Entity Framework Database relation Many to Many如何从实体框架数据库关系多对多中的选择框中保存数据
【发布时间】:2015-12-09 05:34:01
【问题描述】:

如何从实体框架数据库关系多对多的选择框中保存数据

有两个类一个武器和另一个用户..

公共类武器 { } 公共类用户 { }

 public class User
        {              
            public int ID { get; set; }
            public string Name { get; set; }
            public string Type { get; set; }      
            }



 public class Wepon
{   public int ID { get; set; }
    public string Wepon_Name { get; set; }
    public int Power { get; set; }

}

这应该与使用 FormCollection 和模型的多对多相关

【问题讨论】:

  • 武器? :) 你不是说武器吗?
  • lol yp 拼写错误:D

标签: c# entity-framework many-to-many dbcontext


【解决方案1】:

用户类

还有武器类

public class User
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public string Type { get; set; }
            **public List<Wepon> WeposInList { get; set; }**

            }



 public class Wepon
    {
        public int ID { get; set; }
        public string Wepon_Name { get; set; }
        public int Power { get; set; }
        public List<User> UsersHaveWeponsList { get; set; }// User the List for M to M
}
  • DBContext

     public class DbContexFor : DbContext
        {    
            public DbContexFor()
                : base("name=ConnectionStringName")
            {
            }
    
            public virtual DbSet<User> Users { get; set; }
            public virtual DbSet<Wepon> Wepons { get; set; }
        }
        }
    

    **

  • 控制器代码

**

  [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = "ID,Name,Type")] User user, FormCollection formData)
            {

                if (ModelState.IsValid)
                {
                    var ss = formData["ShipFromCountries"].ToString();
 user.WeposInList = db.Wepons.Where(c => c.Wepon_Name == ss).ToList();
                    db.Users.Add(user);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

                return View(user);
            }

还有 HTML 代码

@model enumVarAction.Models.User

@{

var list = ViewBag.MyList;
ViewBag.Title = "Create";

}

创建 Html 页面

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

    <div class="form-horizontal">
        <h4>User</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

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

        <div class="form-group">
            @Html.LabelFor(model => model.WeposInList, htmlAttributes: new { @class = "control-label col-md-2" })
            <select  id="ShipFromCountries" multiple="multiple" name="ShipFromCountries">
                <div class="col-md-10">

                    @foreach (var VARIABLE in list)
                {
                        <option value="@VARIABLE.Wepon_Name">@VARIABLE.Wepon_Name</option>
                    }
                </div>
            </select>
        </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>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

**

  • 在控制器上调试

**

**

  • 数据即数据库

**

【讨论】:

    猜你喜欢
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 2015-07-11
    相关资源
    最近更新 更多