Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 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>
Posted
Updated 23-Mar-17 7:08am

Quote:
what is the difference between these two objects created.


None really.

ViewModel vm = new 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();


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();


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
 
Share this answer
 
Comments
Member 13053943 21-Mar-17 6:07am    
thanks for explanation on the viewmodel,its identical,the second part of my question that,i am posting info into a database,before it is posted,there must be a confirmation message as a popup to display whatever i typed in there before i posted,so i have given my controller and view for you to make suggestions
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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900