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 7553831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:10:10+00:00 2026-05-30T11:10:10+00:00

this is my first time ever posting a question and I am kind of

  • 0

this is my first time ever posting a question and I am kind of new to Visual Studio 2010. I have a web application (for a school assignment) that allows you to create a “news item” that will be displayed on the front page. I have done everything in my assignment except I can’t for the life of me figure out how to have valiadation on my form whenever a textbox on the form is left empty. I have been looking everywhere and have followed a few tutorials but still can’t get it to work with my particular case. I went to my database in the visual designer, right clicked it and clicked “view code” and placed this in there:

using System.ComponentModel.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NewsDisplay
{
  partial class NewsItemDataContext
  {
  }

  [MetadataType(typeof(NewsItem_Validation))]
  public partial class NewsItem
  {

  }

  public class NewsItem_Validation
  {
     [Required(ErrorMessage = "Title is Required")]
     public string Title { get; set; }
  }
}

And then my create function is in my Homecontroller and looks like this:

    [HttpPost]
    public ActionResult Create(FormCollection formData)
    {
        NewsItem n = new NewsItem();
        if (ModelState.IsValid)
        {
            TryUpdateModel(n);
            NewsItemRepository repository = new NewsItemRepository();
            repository.AddNewsItem(n);
            return RedirectToAction("index");
        }
        else
        {
            return View(formData);
        }

    }

At first I would get thrown an exception at UpdateModel, but then I found that using TryUpdateModel works. Now I get thrown an exception at my .aspx code that controls the home page because the Title of the news item cannot be blank. Here is my index.aspx page with a comment at where I get the exception:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<NewsDisplay.NewsItem>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Newest Stories</h2>
<% foreach (var item in Model) { %>
    <table class="center">
        <thead>
            <tr>
                <td>
                    <%= Html.ActionLink( item.Title, "Edit", new { id=item.NewsItemID } ) %> //this is where I get thrown the exception
                </td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td id="topRow">
                    <%: item.Date %>
                </td>
            </tr>
            <tr>
                <td id="bottomRow">
                    <%: item.Details %>
                </td>
            </tr>
        </tbody>
    </table>
<% } %>
</asp:Content>

Am I even on the right track? I have been looking this up for a while now and I just can’t seem to figure out how to make it work for my personal project. Thanks in advance for any help!

  • 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-05-30T11:10:11+00:00Added an answer on May 30, 2026 at 11:10 am

    why u dont use validator on control box?
    u can use validator and set the control that must be validate and define error message on control

    the page never post if data in control not valid

    here is example

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ControlToValidate="TextBox1" ErrorMessage="Write Error Message Here"></asp:RequiredFieldValidator>
    

    solve your problem?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first time posting here, I have a question which I have
I have recently started learning F#, and this is the first time I've ever
This is my first time with Web services. I have to develop web services
I'm am super new at this, and this is my first time ever using
This is the first time ever I'm using AJAX, and I want to do
This was my first time ever trying to save object in a file, so
This is the first time I've ever tried to use JAXB for anything at
This is the first time I have been faced with someone trying to penetrate
This is the first time I've ever tried JQuery basically because I couldnt get
I have used maps many times before, but this is the first time I

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.