Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8136547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:47:40+00:00 2026-06-06T10:47:40+00:00

I am trying to create what I feel is a very simple form submission

  • 0

I am trying to create what I feel is a very simple form submission using a ViewModel. I have worked on this off and on all day and for some reason cannot understand why when my app gets to my HttpPost action my EmailViewModel is empty. I get a “NullReference Exception Occurred” “Object reference not set to an instance of an object” error.

Can you take a look at my code and tell me where I am being crazy?

Here is my httpPost action:

[HttpPost]
public ActionResult SendStudentAnEmail(EmailViewModel email)
{
        Debug.Write(email.Subject); // First NullReferenceException
        Debug.Write(email.Body);
        Debug.Write(email.Email);
        etc. . .

My ViewModel:

namespace MyApp.ViewModels
{
    public class EmailViewModel
    {
        public string Email { get; set; }

        public string Subject { get; set; }

        public string Body { get; set; }
    }
}

and My View:

@model MyApp.ViewModels.EmailViewModel

@{
    ViewBag.Title = "SendStudentAnEmail";
}

<h2>SendStudentAnEmail</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>EmailViewModel</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Subject)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Subject)
            @Html.ValidationMessageFor(model => model.Subject)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Body)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Body)
            @Html.ValidationMessageFor(model => model.Body)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Thank you.

*UPDATE*

If I change my HttpPost Action to use FormCollection, I can use the values just fine, I can even re-cast the FormCollection values back to my EmailViewModel. Why is this?

[HttpPost]
public ActionResult SendStudentAnEmail(FormCollection emailFormCollection)
{
        Debug.Write(emailFormCollection["email"]);
        Debug.Write(emailFormCollection["subject"]);
        Debug.Write(emailFormCollection["body"]);
    var email = new EmailViewModel
                        {
                            Email = emailFormCollection["email"],
                            Subject = emailFormCollection["subject"],
                            Body = emailFormCollection["body"]
                        };
. . . . then the rest of my code works just how I wanted. . . 

Why do I have to cast from FormCollection over to my EmailViewModel? Why isn’t it giving me the NullReference Exception if I attempt to simply push an EmailViewModel into my Action?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:47:41+00:00Added an answer on June 6, 2026 at 10:47 am

    Your EmailViewModel class has a property called Email of type string. And your controller action takes an argument called email of type EmailViewModel. This confuses the default model binder. So either rename the property inside the view model or the action argument:

    [HttpPost]
    public ActionResult SendStudentAnEmail(EmailViewModel model)
    {
        Debug.Write(model.Subject);
        Debug.Write(model.Body);
        Debug.Write(model.Email);
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this very simple wrapper template: template<class T> struct wrapper { inline operator
I'm very new to java and feel very confused. I'm trying to create a
I'm sure what I'm trying is very simple, but I've never quite worked with
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
Trying to create a background-image slideshow and am getting this error... This is the
I feel like the answer to this question is really simple, but I really
I'm trying to create a test class which organizes its test methods using inner
Can anyone shed any light on this? I feel like I have wasted the
I feel like this is a rather simple question, but I can't seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.