我已经基于三个表的组合创建了一个视图模型。
我单击编辑操作,它会正确显示三个表中的数据。
但是当我点击“保存”按钮时,我无法从FormCollection或Request [“Id”]
获取数据请以可能的方式建议。
公共类ConferenceResourceEditModel {
public ConferenceRoom ConferenceRoom { get; set; }
public Resources Resources { get; set; }
public ResourceAllocation ResourceAllocation { get; set; }
}
public ActionResult Edit(int id)
{
// ConferenceRoom conferenceroom = 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);
}
答案 0 :(得分:0)
在http://www.asp.net/mvc网站上查找模型绑定,其中有关于此类事情的精彩教程。
简而言之,您的控制器操作将采用YOURVIEWMODEL类型的参数并自动绑定到它
答案 1 :(得分:0)
使用一些ORM(如EntityFramework,我个人建议采用DatabaseFirst方法)。使用类似的东西:
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
更加复杂和困难。希望它会有所帮助。