views:

45

answers:

1

I saw a similar question here on SO but I believe mine differs a little a bit.

OK, so I have a simple view here:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<RootFinder.Models.QuadCalc>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Polynomial Root Finder - Quadratic
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Quadratic</h2>

    <%= Html.BeginForm("Quadratic", "Calculate") %>
    <% { %>
    <div>
        a: <%= Html.TextBox("quadAValue", Model.quadraticAValue) %>
        <br />
        b: <%= Html.TextBox("quadBValue", Model.quadraticBValue) %>
        <br />
        c: <%= Html.TextBox("quadCValue", Model.quadraticCValue) %>
        <br />
        <input type="submit" id="quadraticSubmitButton" value="Calculate!" />
        <br />
        <p><%= Model.x1 %></p>
        <p><%= Model.x2 %></p>
    </div>
    <% } %>
</asp:Content>

And my controller here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RootFinder.Models;

namespace RootFinder.Controllers
{
    public class CalculateController : Controller
    {
        //
        // GET: /Calculate/

        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ViewResult Quadratic()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ViewResult Quadratic(QuadCalc newQuadCalc)
        {
            return View(newQuadCalc);
        }

        public ActionResult Cubic()
        {
            return View();
        }

        public ActionResult Quartic()
        {
            return View();
        }
    }
}

Now, upon loading my Get version of the Quadratic view, I get the following message from VS2010:

Object reference not set to an instance of an object

Now, I kind of understand the message in it of itself, but isn't it a bad thing to create a new object of a class within the View itself? Which is why I was trying to handle this in the Controller for the Post only.....

Hmm...

+1  A: 

Hi!

Same as you do in your Post action, but in the Get you pass a new fresh initialized QuadCalc model to the view

[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Quadratic() {
    return View( new QuadCalc() );
}
Lorenzo
Hey, thanks again! :) Now, is the syntax you provided produce the same results as the syntax I provided in my Post, just a matter of preference? Or do they mean different things?
BOSS
I am not sure to understand what you mean by sintax :) Do you mean the format? I have just copied your code and formatted as I like. If this was'nt your meaning please let me understand it better. Thanks!
Lorenzo
OK, that's what I mean. I was just referring to the fact that you placed parentheses `()` within the `return view()`. Mine returned the object. But, it's slightly different than the post, like you said, correct?
BOSS
Yes. That's correct. It's slightly different because in the action with the POST verb you already have a model class coming back from the view. You just fill it up with result and send it back. In the GET action, you need a new object to be created, that's the way you have parenthesis. If you create the object outside the `return View()` instruction then you dont need it as well.
Lorenzo
OK, thanks! Helped a lot.
BOSS
you are welcome!
Lorenzo