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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:01:12+00:00 2026-06-09T04:01:12+00:00

<div id=myDiv> <ul class=purpose> @{ var purpose = CommonMethod.getPurpose(); for(int i=0;i<10 && i<purpose.Count();i++) {

  • 0
<div id="myDiv">
    <ul class="purpose">
    @{ 
        var purpose = CommonMethod.getPurpose("");
        for(int i=0;i<10 && i<purpose.Count();i++)
        {
            <li><a href="#" >@purpose[i].Text</a></li>
        }
     }
    </ul>
</div>

on selecting the above single purpose from list it will show below ajax form for updation

@using (Ajax.BeginForm("UpdatePurpose", "Admin", new AjaxOptions { UpdateTargetId = "updateDiv" }))
{
    <div  class="profile">
    @Html.Hidden("PurposeID")
    <table>

        <tr>
             <td>Purpose</td>
             <td>:</td>
             <td> width="30%">@Html.TextBox("Description")</td>         
        </tr>
        <tr>
            <td>Admin Password</td>
            <td>:</td>
            <td>@Html.Password("Passwd1")</td>
        </tr>
    </table>
   <input type="submit"  value="submit" />
   </div>
}

After Subting below action is called

[HttpPost]
public ActionResult UpdatePurpose(string PurposeID,string Description, string Passwd1)
{

    if (Request.IsAjaxRequest())
    {
        if (!Membership.ValidateUser(User.Identity.Name, Passwd1))
        {
            ModelState.AddModelError("", "Invalid Admin Password");
        }
        if (ModelState.IsValid)
        {
            var id = Convert.ToByte(PurposeID);
            var pur = db.Purposes.Where(p => p.PurposeID == id).SingleOrDefault();
            pur.Description = Description;

            db.SaveChanges();
            ViewBag.Result = "Data Updated Successfully.";
            return View();
        }
    }
    return View();
}

On submitting above ajax form Updatepurpose I want to show validation message errors if admin password is invalid and also I want to load myDiv content if modelstate is valid and give successful updation message to user

  • 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-09T04:01:14+00:00Added an answer on June 9, 2026 at 4:01 am

    Remove the Ajax.BeginForm HTML Helper and do some handwritten jQuery code. You can do any kind of customization then.

    I would keep your Markup like this (removed AjaxForm, Used a normal form declaration, Added a css class name to submit button)

    <div  class="profile">
    <form>
    @Html.Hidden("PurposeID")
    <table>
        <tr>
             <td>Purpose</td>
             <td>:</td>
             <td> width="30%">@Html.TextBox("Description")</td>         
        </tr>
        <tr>
            <td>Admin Password</td>
            <td>:</td>
            <td>@Html.Password("Passwd1")</td>
        </tr>
    </table>
    <input type="submit" value="submit" class="btnSavePurpose" />
    

    And add some javascript like this

    <script type="text/javascript">
     $(function(){
    
        $(".btnSavePurpose").click(function(e){
           e.preventDefault();
           var item=$(this);
           $.post("@Url.Action("UpdatePurpose","Admin")", 
                             item.closest("form").serialize(), function(data){
               if(data.Status=="Success")
               {
                 //Let's replace the form with messsage                
                 item.closest(".profile").html("Updated Successfully");
               }    
               else
               {
                  alert(data.ErrorMessage);
               }
    
           });   
        });   
    
     });
    
    </script>
    

    Now update your Action method to return the JSON data

    [HttpPost]
    public ActionResult UpdatePurpose(string PurposeID,string Description, string Passwd1)
    {        
        if (Request.IsAjaxRequest())
        {
            if (!Membership.ValidateUser(User.Identity.Name, Passwd1))
            {
                return Json( new { Status="Error",
                                   ErrorMessage="Invalid Admin Password"});
            }
            if (ModelState.IsValid)
            {
                var id = Convert.ToByte(PurposeID);
                var pur = db.Purposes.Where(p => p.PurposeID == id).SingleOrDefault();
                pur.Description = Description;
    
                db.SaveChanges();
                return Json( new { Status="Success"});
            }
        }
        return View();
    }
    

    What it is doing is, When user clicks on the button with that CSS class, it will serialize the container form and send it to the Action method. Then the action method do its job and if the Validation fails, It will send a JSON in the below format back to the client

     { "Status": "Errorr", "ErrorMessage": "Invalid Admin Password"}
    

    If it is fine, It will do the DB update and send a JSON back to the client in this format.

     { "Status": "Success"}
    

    And in the callback of out ajax(POST) function, we are checking the value of Status in the JSON and showing appropriate message to the user.

    Simple & Clean 🙂

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

Sidebar

Related Questions

I have <span> inside a <div> , like so: <div id=#mydiv> <a href=#>Hello</a><span class=myImageArea></span>
My HTML is something like this <div id=mydiv class=common> <input type=text id=text1 value= />
I have a div <div class=myDiv> <a href=# class=myLink>somelink</a> <div class=anotherDiv>somediv</div> </div> Now, using
With regard to the following markup: <div class=mydiv> <a href=index.php><img src=myimage.gif /></a> </div> The
Using the following code: <a href=/someurl> <div class='mydiv' onMouseOver=document.getElementById('myid').sendEvent('play'); onMouseOut=document.getElementById('myid').sendEvent('stop');> <div id='myid'></div> <script type=text/javascript>
var a = document.createElement('div'); a.id = myDiv; and var a = document.createElement('div').id = myDiv;
<div id=myDiv class= blueberry mango ></div> If we use the .addClass() $(#myDiv).addClass(carrot); The class
.myDiv { background: url(/images/myimage.jpg); margin: 0 auto; } <div class=myDiv> Click Me! </div> I
I have a page containing the following div element: <div id=myDiv class=myDivClass style=>Some Value</div>
Say i have this: <div class='myDiv'> <p>hello</p> hello <p>Hello</p> </div> How does one grab

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.