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 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Can you give me an example please?
i need to plus another hint. in your code behind you need to write this code to check the page is valid or not. if(page.isvalid) { every thing you want to do }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.