使用ASP.NET MVC中的ViewModel在控制器中执行CRUD

问题描述 投票:0回答:2

从我的两个模型StudentInventoryRecord。我创建了一个名为TestViewModel的ViewModel。我很困惑如何编写控制器?

public class TestViewModel
{
    //from Student model
    [Key]
    public string UserID { get; set; }
    public string PhoneNumber{ get; set; }
    public string Address{ get; set; }
    //other properties

    //from Inventory model
    public string FathersName { get; set; }
    public string FathersAddress { get; set; }
    //other properties

}

当我只使用我的主要模型Student时。这就是我编写控制器的方式:

// GET: Students/CreateEdit
    public ActionResult InventoryRecord()
    {
        var currentUserId = User.Identity.GetUserId();
        var newid = db.Students.FirstOrDefault(d => d.UserID == currentUserId);

        if (newid == null)
        {
            newid = db.Students.Create();
            newid.UserID = currentUserId;
            db.Students.Add(newid);

        }

        Student student = db.Students.Find(newid.UserID);
        if (student == null)
        {
            return HttpNotFound();
        }

        return View(student);
    }

    // POST: Students/CreateEdit
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult InventoryRecord(Student student)
    {

        var currentUserId = User.Identity.GetUserId();
        var userName = User.Identity.GetUserName();
        var u = db.Students.FirstOrDefault(d => d.UserID == currentUserId);

        if (u == null)
        {
            u = db.Students.Create();
            u.UserID = currentUserId;
            db.Students.Add(u);

        }

        if (ModelState.IsValid)
        {
            u.PhoneNumber = student.PhoneNumber;
            u.Address = student.Address;
            //other properties

            db.SaveChanges();

            TempData["Message"] = "User: " + userName + ", details successfully updated!";
        }

        return View(student);
    }

现在,我真的很困惑如何在这里继续。如果我使用我的TestViewModel,我该怎么写我的控制器?有人请指出我正确的方向。谢谢。

c# asp.net-mvc viewmodel
2个回答
0
投票

好吧,我个人会把代码移出控制器。

但是,例如,您只需要创建TestViewModel的实例并将其传递给您的视图。如果您在cshtml中特定模型,则可能还需要更新View。

public ActionResult InventoryRecord()
{
    var currentUserId = User.Identity.GetUserId();
    var newid = db.Students.FirstOrDefault(d => d.UserID == currentUserId);

    if (newid == null)
    {
        newid = db.Students.Create();
        newid.UserID = currentUserId;
        db.Students.Add(newid);

    }

    Student student = db.Students.Find(newid.UserID);
    if (student == null)
    {
        return HttpNotFound();
    }

    TestViewModel model = new TestViewModel
    {
        UserID = student.UserId,
        PhoneNumber = student.PhoneNumber,
        //add the rest.
    };

    return View(model);
}

0
投票

而不是返回Student,返回TestViewModel

    Student student = db.Students.Find(newid.UserID);
    if (student == null)
    {
        return HttpNotFound();
    }

    TestViewModel tvm = new TestViewModel()
    {
        UserID =student.Id,
        PhoneNumber = student.PhoneNumber,
        Address= student.Address
    };

    return View(tvm);
 }

第二种方法是

 public ActionResult InventoryRecord(TestViewModel tvm)
© www.soinside.com 2019 - 2024. All rights reserved.