发布不可数值并保存为M-2-M关系

时间:2013-09-09 16:38:37

标签: asp.net-mvc linq entity-framework parameter-passing code-first

VS'12 KendoUI InternetApplication Template C#asp.net EF Code First

我的问题是如何pass Regular(现在正在传递)值和Ienumerable(传递null)到我的controller并将它们保存到Databas {1}}在EF Code First庄园中使用Many-2-Many Relationship

以下是我的尝试

  • 主视图

    @model OG.Models.UserProfiles
    
    @using (Html.BeginForm())
    {   
    
         <div class="editor-field">
    
            <div class="Containter">
            <div>
                 @Html.DisplayFor(model => model.UserName)
            </div>
    
                 <div class="contentContainer">
                 @foreach (var item in Model.Prospects)
                 {
                     <table>
                         <tr>
                             <td>
                                 @Html.Label("Current Prospects")
                            </td>
                         </tr>
                         <tr>
                             <td>
                                @Html.DisplayNameFor(x=>item.ProspectName)
                             </td>
                         </tr>
                     </table>
                 }
    
               </div>
    
             </div>
    
            <div class="contentContainer2">
                 @Html.Partial("_UsersInProspectsDDL", new  OG.ModelView.ViewModelUserInProspects() { Users = Model.UserName })
            </div>
    
        </div>
    }
    
  • 部分视图

     @model OG.ModelView.ViewModelUserInProspects
    
                <label for="prospects">Prospect:</label>
                @(Html.Kendo().DropDownListFor(m=>m.Prospects)
                      .Name("Prospects")
                      .HtmlAttributes(new { style = "width:300px"}) //, id = "countys"})
                      .OptionLabel("Select Prospect...")
                      .DataTextField("ProspectName")
                      .DataValueField("ProspectID")
                      .DataSource(source => {
                          source.Read(read =>
                          {
                              read.Action("GetCascadeProspects", "ChangeUsersInfo")
                                  .Data("filterProspects");
                          })
                          .ServerFiltering(true);
                      })
                      .Enable(false)
                      .AutoBind(false)
                      .CascadeFrom("Clients")
    
    </div>
    

       

  • PartialView模型

    public class ViewModelUserInProspects
        {
        public string Clients { get; set; }
        public IEnumerable<dbClient> AvailableClients { get; set; }
        public string Prospects { get; set; }
        public IEnumerable<dbProspect> AvailableProspects { get; set; }
    
        public string Users { get; set; }
        public IEnumerable<UserProfiles> AvailableUsers {get;set;}
    
        }
    }
    
  • 主要模式

    Standart SimpleMemberShipUserTable
    
  • 发布方法

    [HttpPost]
    public ActionResult UsersInProspect(
        [Bind(Include= "ProspectName, ProspectID")]
        UserProfiles userprofiles, ViewModelUserInProspects values, FormCollection form) 
        //<- Trying different things sofar 
    {
        if (ModelState.IsValid)
        {
    
            //string something = form["Prospects"];
            int prosID = Convert.ToInt16(values.Prospects);
            int  UserID = userprofiles.UserID; // <- THIS VALUE is null atm.
    

这是我需要将ID保存到EF生成/映射表的地方。不确定如何。

            db.Entry(userprofiles).CurrentValues.SetValues(userprofiles);

            db.Entry(userprofiles).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(userprofiles);
    }

1 个答案:

答案 0 :(得分:1)

请查看Here

  • 经过ViewModels
  • EditorTemplate是什么以及如何使用
  • GET Method的样子
  • Edit View的样子
  • 为您提供View示例
  • Post Method的样子