user3681970
user3681970

Reputation: 1271

Can view create model objects in MVC?

I have done some projects on MVC. I have a generic doubt.

  1. Can view create objects of model class in MVC pattern? For example, lets say we are designing MVC for taxi management system. We have classes for cab and passenger. View will be taking passenger and cab details from user.

  2. can we instantiate cab and passenger(model class) in view for storing details?

  3. If we cant instantiate then where should we store details after taking from user?

  4. How will it pass to controller?

    I am new to MVC and any help will be greatly appreciated. I have googled it many times but have not got the satisfactory answer.

Upvotes: 2

Views: 431

Answers (1)

Muhammad Usman
Muhammad Usman

Reputation: 1362

  1. Yes we can create the objects of model class in view (but then we are going to kill the concept of MVC)
  2. We can save the object of model classes
  3. for client side you have to save the values of both class attributes and passs them by using ajax/form (post/get).
  4. How to pass the values Code

View HTML

 <div class="registrationForm">
      @using (Html.BeginForm("Registration", "Car", FormMethod.Post))
      {
          <p>
              <input type="text" name="carName" placeholder="Your Car Name"  />
          </p>
          <p>
              <input type="text"  name="carNum" placeholder="Re-enter Number"  />
          </p>

          <p>
              <input name="signup" type="submit" value="Submit">
          </p>
    }
  </div>

Controller Action

public ActionResult Registration(String carName, string carNum)
{
   // your logic
   return View();

}

Please modify if I am wrong.

Upvotes: 2

Related Questions