2016-05-17 7 views
-1

Ich erhalte einen Fehler, wenn ich auf "Senden" klicke.Fehler wird angezeigt: Ein Artikel mit demselben Schlüssel wurde bereits hinzugefügt

Error Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Meine Ansicht

@model renderview.Models.Registration 


    <div id="body"> 

     <h2>Contact</h2> 

     @using (Html.BeginForm("Registration", "home", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
      @Html.AntiForgeryToken()   // this is to prevent CSRF attack 
               @Html.ValidationSummary(true) 

               <h3>Sign up</h3> 
               <label for="name"> 
                <span>Name</span> 
                @Html.TextBoxFor(model => model.Name) 
                @Html.ValidationMessageFor(model=>model.Name) 

               </label> 
               <label for="email"> 
                <span>Email</span> 
                @Html.TextBoxFor(model => model.Email) 
                @Html.ValidationMessageFor(model=>model.Email) 
               </label> 

               <label for="password"> 
                <span>Pasword</span> 
                @Html.TextBoxFor(model => model.Password, new { @type = "password" }) 
                @Html.ValidationMessageFor(model => model.Password) 
               </label> 
               <label for="Phone"> 
                <span>Phone</span> 
                @Html.TextBoxFor(model => model.Phone) 
                @Html.ValidationMessageFor(model => model.Phone) 
               </label> 
               <label for="Address"> 
                <span>Address</span> 
                @Html.TextAreaFor(model => model.Address, new {style ="width: 100"}) 
                @Html.ValidationMessageFor(model => model.Address) 
               </label> 
      <p>Select Country:</p> 

               @Html.DropDownList("Country", ViewBag.country as SelectList,"Select a Country", new { @id="Country"}); 
               <br /> 
      <p>Select State:</p> 
               <select id="State" name="state"></select><br /> 
    //  @Html.DropDownList("State"); 

      <br /> 
      <br /> 
      <input type="file" name="ImageData" id="ImageData" onchange="fileCheck(this);" /> 
      <br /> 
      <input type="submit" id="send" value="Submit"> 
} 
</div> 
    <script src="~/Scripts/jquery-1.11.1.min.js"></script> 
    <script type="text/javascript" src="~/Scripts/jquery-1.8.2.js"></script> 

<script> 
     $(document).ready(function() { 
      $("#Country").change(function() { 
       var gfhsdf = $("#Country").val(); 
       alert(gfhsdf) 
       var url="@Url.Action("GetStates1", "home")"; 
       $.ajax({ 
        type: 'GET', 
        url: url, 
        data: { id: $("#Country").val() }, 
        success: function (data) { 

         $.each(data, function (i, state) { 
          $("#State").append('<option value=" ' + state.Id + ' ">' + state.Name + '</option>'); 
          //alert(st.Id); 
         }); 
        }, 
        error: function (ex) { 
         alert('Failed to retrieve states.' + ex); 
        } 
       }); 
       return false; 
      }); 
     }); 
    </script> 

mein Controller

 public ActionResult Registration() 
    { 
     DataClassesRegistrationDataContext db = new DataClassesRegistrationDataContext(); 
     List<CountryModel> Countrydropdownlist = new List<CountryModel>(); 

     var q = (from r in db.Countries select r).ToList(); 
     if (q != null) 
     { 
      foreach (var query in q) 
      { 
       CountryModel con = new CountryModel(); 
       con.Id = query.Id; 
       con.Name = query.Name; 
       Countrydropdownlist.Add(con); 
      } 
      ViewBag.country = new SelectList(Countrydropdownlist,"Id","Name"); 
     } 

     return View(); 
    } 


    public JsonResult GetStates1(int id) 
    { 
     DataClassesRegistrationDataContext db = new DataClassesRegistrationDataContext(); 


     var query = (from s in db.tbl_States 
        where id==s.CountryId select s).ToList(); 

     return Json(query,JsonRequestBehavior.AllowGet); 
    } 



    [HttpPost] 
    public ActionResult Registration(Registration _model) 
    { 
     HttpPostedFileBase file = Request.Files["ImageData"]; 
     if (file != null) 
     { 
      string pic = System.IO.Path.GetFileName(file.FileName); 
      string path = System.IO.Path.Combine(
            Server.MapPath("~/Content/images/"), pic); 
      file.SaveAs(path); 
      _model.Image = pic; 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       file.InputStream.CopyTo(ms); 
       byte[] array = ms.GetBuffer(); 
      } 

     } AccountServices service = new AccountServices(); 
     _model.Password = passwordEncrypt(_model.Password); 

     service.Registration(_model); 

     return RedirectToAction("index"); 

    } 
+0

_Bitte überprüfen Sie die Stack-Trace für weitere Informationen über den Fehler und wo es im Code_ –

+0

Comment out 'Service.Registration (_model);'. Tritt derselbe Fehler immer noch auf? –

+0

Debugger trifft nicht auf meine [HTTPPOST] Aktion –

Antwort

2

Dieser Fehler wurde durch einige neue Eigenschaften in Modell hinzugefügt.

-1

Hallo Dhiman, es auf JsonRequestBehavior.AllowGet verlassen konnte. Nimm das nicht.

In Staaten Sie erste Post dann Ajax get und allowGet verwenden. Dies ist ein Ajax Autority-Problem mit net MVC Ich denke, und es ist auch ein doppelter Eintrag. Lassen Sie Staaten in nach und es läuft.

OK - las ich Ihren Kommentar

für die gesamte Anwendung Tracing einfach in system.web so aktiviert: Trace enabled = "true"

OK - ich lese dein Kommentar diesen ersten Versuchen

var url="@Url.Action("GetStates1", "home")"; 
       $.ajax({ 
        type: 'POST', 

und setzen return Json(query,JsonRequestBehavior.DenyGet);

Wenn dies nicht funktioniert, kommentieren Sie den gesamten Status aus und starten Sie die App.

Beste Axel Arnold Bangert - Herzogenrath 2016

+0

Ajax-Aufruf des OP ist ein GET , so ist 'JsonRequestBehavior.AllowGet' erforderlich. Das hat aber nichts mit dem Thema zu tun. –

+0

wo stack trace zu verwenden, bitte helft mir –

+0

@Stephen Mücke –

Verwandte Themen