从类和对象设置对象 [英] Setting object from classes and objects

查看:49
本文介绍了从类和对象设置对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我在下面的代码中键入的内容之间的区别。有时候他们会让我感到困惑。我希望有人也为我解释oop的基础知识。我很难设置对象并使用它们来适合我的工作< br $> b $ b

我尝试了什么:



这两者有什么区别创建的对象。



I want to know the difference between what i type in the code below.sometimes they confuse me.I want someone to also explain the basics of oop for me.i have difficulty in setting objects and using them to suit my work

What I have tried:

what is the difference between these two objects created.

ViewModel vm = new ViewModel();
var vm = new ViewModel();





i必须在一个viewbag中传递值,该值将在我发布到数据库之前显示为确认消息。

这是我的控制器





i have to pass value in a viewbag which will show as a confirmation message before i post in into the database.
this is my controller

public class CoursesStudentController : Controller
    {
        public uniscoreEntities db = new uniscoreEntities();
        

        // GET: CoursesStudent      
        public ActionResult AddCourse(int? id)
        {
                     
            CoursesStudentViewModel VM = new CoursesStudentViewModel();
            Student St = db.Students.Find(id);

            VM.FirstName = St.FirstName;
            VM.LastName = St.LastName;

            VM.Student_Id = St.id_Student;

            VM.Courses = new SelectList(db.Courses, "id_Course", "CourseName");
            

            return View(VM);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddCourse(CoursesStudentViewModel  VM)
        {                      
            if (ModelState.IsValid)
            {
                Student_Course sc = new Student_Course();
                
                int Mark = 0;
                {                        
                    if     (Mark < 100 || Mark > 80) { sc.Grade = "A"; }
                    else if (Mark < 80 || Mark > 70) { sc.Grade = "B"; }
                    else if (Mark < 60 || Mark > 69) { sc.Grade = "C"; }
                    else if (Mark < 50 || Mark > 59) { sc.Grade = "D"; }
                    else if (Mark < 10 || Mark > 49) { sc.Grade = "E"; }
                }
                
               
                    sc.Course_id = VM.Course_Id;
                    sc.Mark = VM.Mark;
                    sc.Student_id = VM.Student_Id;
                    ViewBag.Message = sc;
                         
                db.Student_Course.Add(sc);
                db.SaveChanges();               
                return RedirectToAction("AddCourse");
            }          
            return View(VM);
       
    }
    }
}





这是我的观点





this is my View

@model UniScore.ViewModels.CoursesStudentViewModel

@{
    ViewBag.Title = "AddCourse";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Add Course</h2>
<div>
    <h4>Student</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.FirstName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.FirstName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.LastName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.LastName)
        </dd>
    </dl>
</div>


@using (Html.BeginForm("AddCourse", "CoursesStudent", FormMethod.Post, new { id = "AddCourse", name = "VM" }))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
    @*<input type="hidden" value="@ViewBag.enr" name="id_Student"/>*@ 
    @Html.HiddenFor(model => model.Student_Id)
    <div class="form-group">
        @Html.LabelFor(model => model.CourseName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.Course_Id, Model.Courses, new { htmlAttributes = new { @class = "form-control" } })

            @Html.ValidationMessageFor(model => model.CourseName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Mark, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Mark, new { htmlAttributes = new { @class = "form-control" } })

            @Html.ValidationMessageFor(model => model.Mark, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
                       <input type="submit" value="AddCourse" class="btn btn-default"/>              
                   @*<button onclick="addcourse()">Add Course value="AddCourse"</button>*@ 
                    </div>
                </div>
            </div>
    }
           <div>
                @Html.ActionLink("Back to List", "Index", "Students")
            </div>

推荐答案

Quote:

创建这两个对象之间有什么区别。

what is the difference between these two objects created.





无实在。





None really.

ViewModel vm = new ViewModel();





在这里,您定义了一个名为vm的变量,并将其设置为ViewModel类型,然后将其分配给ViewModel类的新实例。





Here you are defining a variable called vm and making it of type ViewModel, and then you assign it to a new instance of the ViewModel class.

var vm = new ViewModel();





在这里你定义vm为var,这意味着你让编译器计算出类型,而不是输入它。所以编译器所做的是使用一些基本的智能并看到你将一个ViewModel类型的对象分配给vm,所以vm必须是ViewModel,所以它将var改为ViewModel并编译它而不是





Here you are defining vm as "var" which means you are letting the compiler work out the type rather than you entering it. So what the compiler does is use some basic intelligence and sees that you are assigning an object of type ViewModel to vm, so vm must be ViewModel, so it changes "var" to "ViewModel" and compiles this instead

ViewModel vm = new ViewModel();





尽可能看到它与你上一行相同。 var不是像ViewBag那样的类型或动态变量,它只是一个编程辅助工具,让编译器为你输出类型。



至于你的其他帖子,我没有看到相关性,但是如果你想在html中输入那个消息,那么只需将它添加到需要它的视图中





As you can see that is identical to your previous line. "var" isn't a type or a dynamic variable like the ViewBag, it is simply a "programming aid" where you get the compiler to work the type out for you.

As for the rest of your post I didn't see the relevance, but if you want that message in the html then simply add it in the view where it is needed

@ViewBag.Message


阅读发布的答案后,我相信你已经得到了第一个问题的答案。这是为了回答你的第二个问题



您在代码中使用了按钮并调用了一些javascript函数,现在已经注释了。

我建议你对你的输入类型使用相同的方法。

after reading the answer posted, i am sure you have got the answer for your first question. this is to answer your second question

earlier in your code you had used button and calling some javascript function, which is commented now.
I would recommend you to use the same approach for your input type.
<input type="submit" value="AddCourse" onclick="isConfirmed();" class="btn btn-default"/>

<script>
function isConfirmed()
{
if(confirm('are you sure want to change ?'))
return true;
else 
return false;
}
</script>


这篇关于从类和对象设置对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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