如何在mvc控制器中创建视图的重载版本 [英] how to create an overloaded version of a view in mvc controller

查看:78
本文介绍了如何在mvc控制器中创建视图的重载版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我正在mvc3 web apllication上进行在线录取项目。



i创建了一类学生,并使用一个inerface作为中间人之间的中间人web apllication和我的域名完美保存我的记录。我使用了一个视图向用户呈现表单并将记录保存到数据库中。保存方面工作正常。



但是我想要完成的是保存学生想要2 cahange一些信息,我希望记录被覆盖并且不会在数据库中重新保存。



所以我想创建一个重载版本的视图来实现它。



我试了一下,我得到一个错误,说它无法执行或绑定null参考。

然后我意识到错误是正确的,因为在学生可以编辑之前必须首先保存记录。



但我不能似乎编辑找到一种方法来编辑记录。任何帮助将不胜感激cos am stock ..我的代码如下。







  public  ViewResult注册( int  id)
{
学生= studentrepository.Student.FirstOrDefault(p => p.ID == id);



return 查看(学生);
}
[HttpPost]
public ActionResult注册(学生)
{
if (ModelState.IsValid)
{
studentrepository。 Save (student);

}



return (学生);




}

解决方案

要么有一个标志如果条目是新的,则明确地告诉控制器。或者在保存之前检查学生是否已经存在(您需要一个学生特有的数据,如注册号或SSN)


hey guys,am working on an online admission project in mvc3 web apllication.

i have created a class of students and use an inerface as the middle man between the web apllication and my domain to save my records perfectly.i have used a view to present the form to the users and save the record into the database.The saving aspect works perfectly.

But what i want to accomplish is surposing the student want 2 cahange some information after saving,i want the records to be overwritten and not resaved in the database.

so i want to create an overloaded version of my view to accomplish that.

I tried it and i got an error that says that it cannot perform or bind a null reference.
Then i realise the error is correct becos the record has to be saved first before the student can edit.

however i cant seem to edit find a way around editing the record. any help will be appreciated cos am stock..here is my code below.



public ViewResult Registration(int id)
    {
        Student student = studentrepository.Student.FirstOrDefault(p => p.ID == id);



        return View(student);
    }
   [HttpPost]
    public ActionResult Registration(Student student)
    {
        if (ModelState.IsValid)
        {
            studentrepository.Save(student);

        }



        return View(student);




    }

解决方案

Either have a flag that tells the controller explicitely if the entry is new or not. Or check before saving if the student already exists (you'll need one datum that is unique to a student such as registration number or SSN)


这篇关于如何在mvc控制器中创建视图的重载版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆