Asp.net mvc 获取错误:已添加具有相同密钥的项
单击“提交”时出现错误 错误描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 我的观点Asp.net mvc 获取错误:已添加具有相同密钥的项,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,单击“提交”时出现错误 错误描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 我的观点 @model renderview.Models.Registration <div id="body"> <h2>Contact</h2> @using (Html.BeginForm("Registration", "home", FormMethod.Post,
@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>
@model renderview.Models.Registration
接触
@使用(Html.BeginForm(“注册”,“主页”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
@Html.AntiForgeryToken()//这是为了防止CSRF攻击
@Html.ValidationSummary(true)
注册
名称
@Html.TextBoxFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
电子邮件
@Html.TextBoxFor(model=>model.Email)
@Html.ValidationMessageFor(model=>model.Email)
Pasword
@Html.TextBoxFor(model=>model.Password,新的{@type=“Password”})
@Html.ValidationMessageFor(model=>model.Password)
电话
@Html.TextBoxFor(model=>model.Phone)
@Html.ValidationMessageFor(model=>model.Phone)
地址
@Html.TextAreaFor(model=>model.Address,新的{style=“width:100”})
@Html.ValidationMessageFor(model=>model.Address)
选择国家:
@Html.DropDownList(“Country”,ViewBag.Country作为选择列表,“选择一个国家”,新建{@id=“Country”});
选择状态:
//@Html.DropDownList(“State”);
}
$(文档).ready(函数(){
$(“#国家”)。更改(功能(){
var gfhsdf=$(“#国家”).val();
警报(gfhsdf)
var url=“@url.Action(“GetStates1”、“home”)”;
$.ajax({
键入:“GET”,
url:url,
数据:{id:$(“#Country”).val()},
成功:功能(数据){
$。每个(数据、函数(i、状态){
$(“#State”).append(“”+State.Name+“”);
//警报(st.Id);
});
},
错误:函数(ex){
警报(“检索状态失败”。+ex);
}
});
返回false;
});
});
我的控制器
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");
}
public ActionResult注册()
{
DataClassesRegistrationDataContext db=新DataClassesRegistrationDataContext();
List Countrydropdownlist=新列表();
var q=(从db.Countries中的r中选择r.ToList();
如果(q!=null)
{
foreach(q中的var查询)
{
CountryModel con=新CountryModel();
con.Id=query.Id;
con.Name=query.Name;
Countrydropdownlist.Add(con);
}
ViewBag.country=新选择列表(Countrydropdownlist,“Id”、“名称”);
}
返回视图();
}
public JsonResult GetStates1(int-id)
{
DataClassesRegistrationDataContext db=新DataClassesRegistrationDataContext();
var query=(来自db.tbl_状态中的s)
其中id==s.CountryId选择s.ToList();
返回Json(查询,JsonRequestBehavior.AllowGet);
}
[HttpPost]
公共行动结果注册(注册模式)
{
HttpPostedFileBase file=Request.Files[“ImageData”];
如果(文件!=null)
{
字符串pic=System.IO.Path.GetFileName(file.FileName);
字符串路径=System.IO.path.Combine(
Server.MapPath(“~/Content/images/”,pic);
file.SaveAs(路径);
_model.Image=pic;
使用(MemoryStream ms=new MemoryStream())
{
file.InputStream.CopyTo(ms);
字节[]数组=ms.GetBuffer();
}
}AccountServices服务=新的AccountServices();
_model.Password=passwordEncrypt(_model.Password);
服务注册(_模型);
返回操作(“索引”);
}
你好,迪曼,
它可能依赖于JsonRequestBehavior.AllowGet。不要拿那个
在状态中首先使用post,然后使用ajax get和allowGet。我想这是ajaxt autority与NetMVC之间的问题,也是双重输入。离开状态在后 var url="@Url.Action("GetStates1", "home")";
$.ajax({
type: 'POST',