获取错误:已添加具有相同键的项目

时间:2016-05-17 06:05:43

标签: 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, 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>

我的控制器

        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");

    }

2 个答案:

答案 0 :(得分:2)

此错误是由于模型中添加了一些新属性。

答案 1 :(得分:-1)

嗨Dhiman, 它可能依赖于JsonRequestBehavior.AllowGet。不要那样。

状态中,您使用第一个帖子,然后使用ajax get和allowGet。这是我猜的网络MVC的ajaxt autority问题,它也是一个双重条目。在发布中保留状态,然后就会运行。

好的 - 我看了你的评论

只需在system.web中启用对整个应用程序的跟踪: trace enabled =“true”

好的 - 我看了你的评论 先试试这个

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

并设置return Json(query,JsonRequestBehavior.DenyGet);

如果这不起作用,请注释掉整个状态并运行应用程序。

最佳

Axel Arnold Bangert - Herzogenrath 2016

相关问题