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");
}
_Bitte überprüfen Sie die Stack-Trace für weitere Informationen über den Fehler und wo es im Code_ –
Comment out 'Service.Registration (_model);'. Tritt derselbe Fehler immer noch auf? –
Debugger trifft nicht auf meine [HTTPPOST] Aktion –