【问题标题】:How to edit using ViewData model如何使用 ViewData 模型进行编辑
【发布时间】:2012-11-23 09:45:25
【问题描述】:

我创建了一个基于三个表组合的视图模型。

我点击编辑操作,它会正确显示三个表中的数据。

但是当我点击保存按钮时,我无法从 FormCollection 或 Request["Id"] 获取数据

请提出可行的方法。

公共类 ConferenceResourceEditModel {

    public ConferenceRoom ConferenceRoom { get; set; }

    public Resources Resources { get; set; }

    public ResourceAllocation ResourceAllocation { get; set; }

}

公共 ActionResult 编辑(int id)
{

//会议室会议室 = db.ConferenceRooms.Find(id);

        var query =

               from c in db.ConferenceRooms

               from r in db.Resourcess

               from ra in db.ResourceAllocation

               where c.ConferenceID == id


               where c.ConferenceID == ra.ConferenceID 

               where r.ResourceID ==ra.ResourceID 

               select new ConferenceResourceEditModel { ConferenceRoom = c, Resources = r,ResourceAllocation=ra };

        return View(query);       
    }

    //
    // POST: /ConferenceRoom/Edit/5

    [HttpPost]
    public ActionResult Edit(FormCollection form, int id, ConferenceResourceEditModel conferenceroom,ConferenceRoom crf)
    {

        if (ModelState.IsValid)
        {
            db.Entry(conferenceroom).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(conferenceroom);
    }

【问题讨论】:

  • 请出示您正在使用的代码。
  • 请建议从 FormCollection 中获取值的方法,因为它显示为空白

标签: asp.net asp.net-mvc asp.net-mvc-3


【解决方案1】:

http://www.asp.net/mvc 网站上查找模型绑定,那里有关于这类事情的精彩教程。

简而言之,您的控制器操作将采用 YOURVIEWMODEL 类型的参数并自动绑定到它

【讨论】:

    【解决方案2】:

    使用一些 ORM(比如EntityFramework,我个人推荐DatabaseFirst 方法)。使用类似的东西:

    var 查询 =

               from c in db.ConferenceRooms
    
               from r in db.Resourcess
    
               from ra in db.ResourceAllocation
    
               where c.ConferenceID == id
    
    
               where c.ConferenceID == ra.ConferenceID 
    
               where r.ResourceID ==ra.ResourceID 
    

    要复杂和困难得多。希望它会有所帮助。

    【讨论】:

    • 在上述情况下是否可以获取 formCollection 或 Viewdatamodel 中的值
    • 当然。看这里:pluralsight.com/training/Courses/TableOfContents/…,尤其是“在 ASP.NET MVC 3 中处理数据”部分。很好的解释,如何使用 ORM 创建良好的控制器/视图工作流
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2011-04-22
    • 2017-10-25
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多