如何知道是什么导致我的操作遇到多个线程?

时间:2019-02-16 12:46:00

标签: c# asp.net-mvc multithreading

我有一个简单的表单提交,包含大约5个字段,提交时它运行到4个线程中,因此在单个post方法调用中将4个条目保存在表中。 我已附上一张图像和代码以说明我所面临的问题的相关性。

我的控制器:

public ActionResult Test(CustomerOrderVM CustomerOrderVM)
{
    try
    {
        if (CustomerOrderVM.Email == "")
        {
            ModelState.AddModelError("Email", "Please enter valid Country");
        }

        if (CustomerOrderVM.City == "" || CustomerOrderVM.City == "< Select City >")
        {
            ModelState.AddModelError("City", "Please enter valid City");
        }

        if (CustomerOrderVM.Contact == "")
        {
            ModelState.AddModelError("Contact", "Please enter valid Contact Number");
        }

        if (CustomerOrderVM.FullName == "")
        {
            ModelState.AddModelError("FullName", "Please enter valid FullName");
        }

        if (ModelState.IsValid)
        {
            tblCustomerOrder _tblCustomerOrder = new tblCustomerOrder();

            _tblCustomerOrder.FullName = CustomerOrderVM.FullName;
            _tblCustomerOrder.Address = CustomerOrderVM.Address;
            _tblCustomerOrder.City = CustomerOrderVM.City;
            _tblCustomerOrder.Contact = CustomerOrderVM.Contact;
            _tblCustomerOrder.Email = CustomerOrderVM.Email;
            _tblCustomerOrder.Proceeded = false;

            db.tblCustomerOrders.Add(_tblCustomerOrder);

            db.SaveChanges();

            return Json(CustomerOrderVM);
        }
        else
        {
            return Json("");
        }
    }
    catch (Exception)
    {
        ViewBag.Error = "Error";
        return Json(ViewBag.Error);
    }
}

我的ViewModel:

public class CustomerOrderVM
{
    public int Id { get; set; }

    [Required]
    [RegularExpression(@"^[a-zA-Z\s]+$", ErrorMessage = "Please enter valid name")]
    [Display(Name = " Name")]
    [StringLength(200, ErrorMessage = "Please enter valid name", MinimumLength = 3)]
    public string FullName { get; set; }

    [DataType(DataType.EmailAddress)]
    [StringLength(150, ErrorMessage = "Please enter valid Email Id", MinimumLength = 5)]
    [Display(Name = "Email ID")]
    public string Email { get; set; }

    [Required]
    [RegularExpression("^[0-9]*$", ErrorMessage = "Please enter valid  Mobile No")]
    [StringLength(15, ErrorMessage = "Please enter valid Mobile No", MinimumLength = 9)]
    [Display(Name = "Mobile No")]
    public string Contact { get; set; }

    [Required]
    [Display(Name = "Street Address")]
    [StringLength(200, ErrorMessage = "Please enter valid address", MinimumLength = 7)]
    [DataType(DataType.MultilineText)]
    public string Address { get; set; }

    [Required]
    [Display(Name = "City")]
    public string City { get; set; }

    [Display(Name = "Proceeded")]
    public bool? Proceeded { get; set; }
}

我的观点:

@model applicaton.Models.ViewModels.CustomerOrderVM

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>



 @using (Ajax.BeginForm("Index", "Home", new AjaxOptions
    {
        HttpMethod = "POST",
        OnSuccess = "OnSuccess",
        OnFailure = "OnFailure",
        LoadingElementId = "progress"
    }))
    {

        <input type="hidden" name="product" value="fent">
        <input type="hidden" name="affiliate" value="">
        <input type="hidden" name="page" value="index">
        <input type="hidden" name="short_referrer" value="">
        <input type="hidden" name="long_referrer" value="">
        <input type="hidden" name="ref" value="">
        <input type="hidden" name="free" value="">
        <input type="hidden" name="discount" value="">
        <input type="hidden" name="avial" id="avial" value="0">
        <input type="hidden" name="pack" id="pack" value="">


 <div class="form-box">
    @Html.EditorFor(model => model.FullName, new { htmlAttributes = new { @class = "form-control MaxWidth uppercase", @placeholder = "Name" } })
    @Html.ValidationMessageFor(model => model.FullName, "", new { @class = "text-danger" })

    </div>



     <div class="form-box">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control MaxWidth uppercase", @placeholder = "Email" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
        </div>


     <div class="form-box">
                @Html.EditorFor(model => model.Contact, new { htmlAttributes = new { @class = "form-control", @placeholder = "Contact : 923xxxxxxxxx", @maxlength = "12" } })
                @Html.ValidationMessageFor(model => model.Contact, "", new { @class = "text-danger" })
            </div>
            <div class="form-box">
               @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control MaxWidth uppercase auto-text-area", @placeholder = "Street Address" } })
                @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
            </div>
            <div class="form-box">
                @Html.DropDownListFor(m => m.City, new SelectList(new string[] {"< Select City >",//Cities}, "< Select City >"), new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })



  </div>
    <button type="submit" class="button redcolor large `enter code here`submit-form">Place My Order!</button>

}

这是一个问题,黄色荧光笔下方附着

指向四个线程

从浏览器生成的POST方法调用 enter image description here

2 个答案:

答案 0 :(得分:0)

可能分别为“电子邮件”,“城市”,“联系人”和“全名”调用Test(...)方法。具有4个线程和4个属性的if-s可能不是巧合。

答案 1 :(得分:0)

找到错误并解决。我错误地在父级和子级cshmtl(layout)页面上提到了Jquery链接(引用),这多次触发了该事件。

感谢所有在“评论”部分中指导我完成工作的人。