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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:41:39+00:00 2026-05-17T01:41:39+00:00

I have a partial view where I render, if the user has choosen an

  • 0

I have a partial view where I render, if the user has choosen an option, a button that permit the user to generate automatically a value for a certain field. Please give a look at this picture to understand what I mean:

alt text

This is achieved using the following markup on the partial view

<%= Html.LabelFor( model => model.IssueCode )%>
<br />
<% if ( Model.HasCodeGenerator ) { %>
    <%= Html.TextBoxFor( model => model.IssueCode, new { style = "width:120px;background-color:#eeeeee;border: solid 2px #dfdfdf", @readonly = "readonly" } )%>
    <% if (Model.ModelState == ModelStateEnum.Add) { %>
        <button id="codeGenerator" style="font-size: 0.7em;margin-right: 10px">Genera codice fascicolo</button>
    <% } %>
<% } else { %>
    <%= Html.TextBoxFor(model => model.IssueCode, new { style="width: 120px" })%>
<% } %>
<%= Html.ValidationMessageFor(model => model.IssueCode, "*")%>

As you can see I append always an Html.ValidationMessageFor() at the end of the input field and a ValidationSummary aut the end of the view.

When the user submits the form the first block of code executed by the action is the following

if ( !ModelState.IsValid ) {
    //Invalid - redisplay form with errors
    return PartialView( "IssueCodeGenerator", model );
}

and this is the result I am getting in all of the three cases

alt text

Why the markup code for the button disappear?

Thanks for helping!

1st EDIT:

After validation the IssueCode textbox loose it’s readonly=”readonly” attribute. This means that the first condition is not meet, I think….

2nd EDIT:

As per the Darin comment I am including

  1. The action that show the Partial View
  2. An extract of the partial that show that the ModelState variable is kept as an hidden control inside the form
  3. The controller Action called by the Partial
  4. The jQuery code that submit the partial

1 – This is the action that shows the partial

[HttpGet]
public ActionResult Create()
{
    IssueModel im = new IssueModel()
    {
        ModelState = ModelStateEnum.Add,
        FirmID = _firmManager.GetMyFirmID(),
        CreatedDate = DateTime.Now,
        LastUpdateDate = DateTime.Now,
        HasCodeGenerator = _optionManager.HasIssueCodeGenerator()
    };
    return PartialView("Issue", im);
}

2 – Extract of the partial Issue.ascx

<% using (Html.BeginForm("SaveOrDelete", "Issue", FormMethod.Post, new { id = "crudForm" })) { %>
    <%= Html.HiddenFor(model => model.FirmID) %>
    <%= Html.HiddenFor(model => model.IssueID) %>
    <%= Html.HiddenFor(model => model.ModelState) %>

3 – This is the controller action called when the form is submitted

[HttpPost]
public ActionResult SaveOrDelete( IssueModel model ) {
    if ( !ModelState.IsValid ) {
        //Invalid - redisplay form with errors
        return PartialView( "Issue", model );
    }

    try {
        Issue i = null;
        if ( model.ModelState == ModelStateEnum.Add )
            i = new Issue();
        else
            i = _manager.FindIssueByIssueID( model.IssueID );
        if ( model.ModelState != ModelStateEnum.Delete ) {
            _manager.BindIssueModel( i, model );
            if ( model.ModelState == ModelStateEnum.Add )
                i.FirmID = _contactManager.GetMyContact().FirmID;
            i.LastUpdateDate = DateTime.Now;
            _manager.SaveIssue( i );
        } else {
            _manager.DeleteIssue( i );
        }
        return PartialView( "ActionCompleted" );
    }
    catch ( Exception ex ) {
        return PartialView( "ActionError",
            new ActionErrorModel() { Message = ex.Message } );
    }
}

4 – This is the jQuery code that submit the form

$("#crudForm").submit(function(event) {
    event.preventDefault();
    $("#crudForm").block();
    $.ajax({
        type: "post",
        dataType: "html",
        url: "/Issue/SaveOrDelete",
        sync: true,
        data: $("#crudForm").serialize(),
        success: function(response) {
            $("#crudForm").parent().html('').html(response);
            $("#crudForm").unblock();
        },
        error: function(response) {
            $("#crudForm").unblock();
        }
    });
});

Hope that this would help in finding the problem. Thank you.

  • 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-17T01:41:40+00:00Added an answer on May 17, 2026 at 1:41 am

    Because Model.ModelState == ModelStateEnum.Add evals to false?

    MVC won’t turn off your button html…


    Even without seeing the form its pretty clear that Darin is right and your Modelstate enum is getting set to null.

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

Sidebar

Related Questions

I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part
I have a snippet to create a 'Like' button for our news site: <iframe
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have found this example on StackOverflow: var people = new List<Person> { new
I have a login.jsp page which contains a login form. Once logged in the
i have a input tag which is non editable, but some times i need
Let say I have the following desire, to simplify the IConvertible's to allow me
I want to have generalised email templates. Currently I have multiple email templates with

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.