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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:13:10+00:00 2026-06-06T14:13:10+00:00

Razor: @Html.TextBoxFor(kod => kod.Name) @Html.ValidationMessage(Name,Client Error Message) Controller: [HttpPost] public JsonResult JsonAddCustomer(Customers customer, string

  • 0

Razor:

@Html.TextBoxFor(kod => kod.Name)
@Html.ValidationMessage("Name","Client Error Message")

Controller:

[HttpPost]
    public JsonResult JsonAddCustomer(Customers customer, string returnUrl)
    {
        if (customer.Name.Trim().Length == 0)
        {
            ModelState.AddModelError("Name", "Server Error Message");
        }

        //Eğer hata yoksa veri tabanına kayıt yapılıyor.
        if (ModelState.IsValid)
        {
            try
            {
                CusOpp.InsertCustomer(customer);
                return Json(new { success = true, redirect = returnUrl });
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", "Error");
            }
        }

        return Json(new { errors = GetErrorsFromModelState() });
    }

I want to write validation error message. I did this like above, but @Html.ValidationMessage("Name","Client Error Message") does not work. In fact, I was already expecting it.

I want to show like this statement’s result: @Html.ValidationMessageFor(m => m.name) ,but I cant use this, because I used entity-data-model.

Should I add [Required] statement to data-model classes or any way that I do this. Sorry for bad explanation.

Thanks.

  • 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-06T14:13:12+00:00Added an answer on June 6, 2026 at 2:13 pm

    You should return PartialViews instead of JSON in this case. Only in the case of success you could return JSON:

    [HttpPost]
    public ActionResult JsonAddCustomer(Customers customer, string returnUrl)
    {
        // Warning: the following line is something horrible => 
        // please decorate your view model with data annotations or use
        // FluentValidation.NET to validate it. 
        // Never write such code in a controller action.
        if (customer.Name.Trim().Length == 0)
        {
            ModelState.AddModelError("Name", "Server Error Message");
        }
    
        //Eğer hata yoksa veri tabanına kayıt yapılıyor.
        if (ModelState.IsValid)
        {
            try
            {
                CusOpp.InsertCustomer(customer);
                return Json(new { success = true, redirect = returnUrl });
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", "Error");
            }
        }
    
        return PartialView(customer);
    }
    

    Now inside the success callback of your AJAX request you can test whether the POST action succeeded or not:

    success: function(result) {
        if (result.redirect) {
            // we are in the success case => redirect
            window.location.href = result.redirect; 
        } else {
            // a partial view with the errors was returned => we must refresh the DOM
            $('#some_container').html(result);
    
            // TODO: if you are using unobtrusive client side validation here's 
            // the place to call the $.validator.unobtrusive.parse("form"); method in order 
            // to register the unobtrusive validators on the newly added contents
        }
    }
    

    Here’s a similar post that you might also read through.

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

Sidebar

Related Questions

Here is the example: http://demos.telerik.com/aspnet-mvc/razor/grid/ajaxbinding @(Html.Telerik().Grid<AXBCore.AccountTemplate>() .Name(Grid1) .Columns(columns => { columns.Bound(p => p.Name).Width(110); columns.Bound(p
I have the following razor template html, although I can't figure out how to
Is it possible to render Razor in Html.Raw()? I have a dynamic page being
How may I add HTML inside Razor tags? I would like to add a
I've created a new html helper method for creating image tags within the razor
I have a TextArea() control in my View implemented using Razor Engine. @Html.TextArea(EventNature,new {
I use razor engine like this: public class EmailService : IService { private readonly
Data : var countries = new Dictionary<string,string>(); countries.Add(AF,Afghanistan); countries.Add(US,United States); countries.Add(FR,France); Razor : @Html.DropdownList(Countries,new
I have the following razor code: <form id=logon action=/security/dev method=get> @Html.AntiForgeryToken() @Html.EditorFor(x => x.UserName)
I have this Razor Template <table> <tr> <td>@Html.RadioButtonFor(i => i.Value, 1)</td> <td>@Html.LabelFor(i => i.Value,

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.