Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me regarding this problem i enable to solve this and nobody can help me regarding this problem

C#
[HttpPost]
      public ActionResult Create(RegisterationVieModel model, HttpPostedFileBase imageUpload)
      {
          string message = "";
          if (imageUpload != null)
          {
              string pic = System.IO.Path.GetFileName(imageUpload.FileName);
              string path = Server.MapPath("~/Images/" + pic);
              imageUpload.SaveAs(path);
          }
          if (ModelState.IsValid)
          {
              message = _Data.Insert(model, imageUpload);
              //return Json(new{message});
              return Json(new { message=message });
          }
          else
          {
              message = _Data.Insert(model, imageUpload);
              return new JsonResult { Data = message };
         }
      }






And View IS:


XML
@model StudentRegisterationForm.ViewModel.RegisterationVieModel
@{
    HtmlHelper.ClientValidationEnabled = true;
    ViewBag.Title = "Create";
  //  Layout = "~/Views/Shared/_LayoutPage1.cshtml";
    
}


@using (Html.BeginForm("Create", "RegisterForm", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    Html.ValidationSummary(false, "Please Resolve The Following Error","Error");
    //Html.ValidationSummary(true);
   <div>
    <table class="table">
        <tr>
            <td>@Html.DisplayNameFor(model=>model.RegisterForm.StudenName)</td>
            <td>@Html.TextBoxFor(model=>model.RegisterForm.StudenName)</td>
            <td>@Html.ValidationMessageFor(model=>model.RegisterForm.StudenName)</td>
        </tr>
        <tr>
            <td>@Html.DisplayNameFor(model=>model.RegisterForm.Age)</td>
            <td>@Html.TextBoxFor(model=>model.RegisterForm.Age)</td>
                <td>@Html.ValidationMessageFor(model=>model.RegisterForm.Age)</td>
        </tr>
        <tr>
            <td>Male</td>
            <td>@Html.RadioButtonFor(model => model.RegisterForm.Gender, "Male")</td>
           
            <td>Female</td> <td>@Html.RadioButtonFor(model => model.RegisterForm.Gender, "Female")</td>
                <td>@Html.ValidationMessageFor(model=>model.RegisterForm.Gender)</td>
        </tr>
        <tr>
            
            <td>@Html.DisplayNameFor(model=>model.RegisterForm.Email)</td>
            <td>@Html.TextBoxFor(model=>model.RegisterForm.Email)</td>
                <td>@Html.ValidationMessageFor(model=>model.RegisterForm.Email)</td>
        </tr>
        <tr>
            <td>@Html.DisplayNameFor(model=>model.RegisterForm.Password)</td>
            <td>@Html.PasswordFor(model=>model.RegisterForm.Password)</td>
                <td>@Html.ValidationMessageFor(model=>model.RegisterForm.Password)</td>
        </tr>
        <tr>
            <td>@Html.DisplayNameFor(model=>model.RegisterForm.Address)</td>
            <td>@Html.TextAreaFor(model=>model.RegisterForm.Address)</td>
            <td>@Html.ValidationMessageFor(model=>model.RegisterForm.Address)</td>
        </tr>
        <tr>
            <td>@Html.DisplayNameFor(model=>model.RegisterForm.Image)</td>
            <td><input type="file" id="imageUpload" name="imageUpload" /></td>
        </tr>
        <tr>
            <td>
                @Html.DisplayNameFor(model => model.RegisterForm.city.CityName)
            </td><td>Countary</td>
            <td>@Html.DropDownListFor(model => model.RegisterForm.CountaryID, new SelectList(Model.CountaryList, "CountaryID", "CountaryName"), "--Select--")</td>
             <td>@Html.ValidationMessageFor(model=>model.RegisterForm.CountaryID)</td>
             <td>State</td><td>@Html.DropDownListFor(model => model.RegisterForm.StateID, new SelectList(Model.StateList, "StateID", "StateName"), "---Select--")</td>
            <td>@Html.ValidationMessageFor(model=>model.RegisterForm.StateID)</td>
            <td>City</td><td>@Html.DropDownListFor(model => model.RegisterForm.CityID, new SelectList(Model.CityList, "CityID", "CityName"), "---Select")</td>
            <td>@Html.ValidationMessageFor(model=>model.RegisterForm.CityID)</td>
        </tr>
        <tr><td><input type="submit" id="insert" value="Submit" /></td></tr>
       
    </table>
     </div>
     <div id="Error">
            @Html.ValidationSummary(false)
        </div>
    }

@*Script Portion of Student Registeration Project *@

    @section script{
       <script src="~/Scripts/jquery-ui.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
@*<script src="~/Scripts/jquery-1.9.1.js"></script>*@
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
         <script type="text/javascript">
             $(document).ready(function () {
                 $("#insert").click(function () {
                         var dataObject =
                         {
                             StudenName: $("#StudentName").val(),//}fetch the values 
                             Age: $("#Age").val(),               
                             Email: $("#Email").val(),
                         }

                         $.ajax(
                  
                             {
                         type: "POST",
                        @* url: '@Url.Action("Create", "RegisterForm")',*@
                         url: '/RegisterForm/Create',
                    data: dataObject,
                    ContentType: "application/json",
                    dataType: "json",
                         
                    success: function (data) {
                        if (data == "Save") {
                            alert("Data Save");
                        }
                        if (data == "Exist") {
                            alert("Data Exist");
                        }
                    },
                    error: function (data) {
                        if (data.toString() == "Not Save") {
                            alert("Data Not Save");
                        }
                    },
                })

            })
        })
    </script>
}
Posted
Updated 28-Jul-15 0:17am
v3

1 solution

Your js code doesn't stop the form from submitting which is why you see the result in the browser too (it runs your js and then submits the form), if you want to stop the form submitting you need to call preventDefault in your click event, and have it "return false;" also.

http://www.w3schools.com/jquery/event_preventdefault.asp[^]

You're better attaching your code to the form submit event rather than the button click, and you're also not going to be able to upload files using that code.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900