0

I have a form in a Create View, for some reason it does not submit and hence does not call the Create controller. If I use the exact same view but for the Edit controller it works perfectly and both Create and Edit controller methods take the same parameters and they have the same attributes and they have almost the same code so I have no clue why the form in the Create view doesn´t submit but the form in the Edit view does. I think it is an error in the JQuery client side validation but no error is displayed. I have tried everything but I'm out of ideas and I have no idea how to debug the JQuery Validation plugin to see in which field of the form it goes wrong and whether it prevents the form submit. What could make the form not submit?

This is the Create view which does not submit:

@model WebContingencia.Models.Contacto

@{
    ViewBag.Title = "Crear nuevo";
}

<h2>Crear</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
@using (Html.BeginForm("Create", "Contacto", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Contacto</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Nombre)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Nombre)
            @Html.ValidationMessageFor(model => model.Nombre)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Celular)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Celular)
            @Html.ValidationMessageFor(model => model.Celular)
        </div>        <p>
            <input type="submit" value="Crear" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

And the model for this view:

public class Contacto
    {
        public int ID { get; set; }
        [StringLength(40, ErrorMessage = "Nombre demasiado largo"), Required(ErrorMessage = "Debe ingresar su nombre y apellido")]
        [Display(Name="Nombre y apellido")]
        public string Nombre { get; set; }
        [StringLength(40, ErrorMessage = "E-mail demasiado largo")]
        [Display(Name = "E-mail")]
        [DataType(DataType.EmailAddress)]
        [RegularExpression(@"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b",ErrorMessage="El e-mail no es valido")]            
        [Remote("ValidarMail", "Contacto", HttpMethod = "Post", AdditionalFields="ID",ErrorMessage = "El e-mail ingresado ya existe")]
        public string Email { get; set; }
        [StringLength(12, ErrorMessage = "Demasiados digitos"), Required(ErrorMessage = "Debe ingresar su telefono celular")]
        [Display(Name = "Telefono celular")]
        public string Celular { get; set; }
        public virtual ICollection<Record> Records { get; set; }
    }

And the create controller. This doesn't get called. I think the form does not even submit so I doubt it the problem is here:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(
            [Bind(Include = "ID,Nombre, Email, Celular")]
            Contacto contacto)
        {
            var record= new Record();
            record.ContactoID = contacto.ID;
            record.Operacion = "CREAR";
            record.Fecha = DateTime.Now;
            record.CelCargado = contacto.Celular;
            record.EmailCargado = contacto.Email;
            contacto.Records=new List<Record>();
            contacto.Records.Add(record);
            try
            {

                if (ModelState.IsValid)
                {
                    db.Contactos.Add(contacto);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    throw new DataException();
                }
            }
            catch (DataException /*dex*/)
            {
                //Uncomment dex and write here to write to log
                ModelState.AddModelError("", "No se pudo guardar los cambios. Por favor intente de nuevo");
            }
            return View(contacto);
        }
4

2 回答 2

0

您的控制器是否命名为“Contacto”?

该行Html.BeginForm("Create", "Contacto", FormMethod.Post)表示调用 Contacto 控制器上的 Create 方法,因此如果您的控制器名称与模型名称不同,则需要将其替换为控制器名称。这就是为什么您应该发布您的控制器代码以及 Stephen Muecke 提到的原因。

于 2014-10-10T13:59:00.363 回答
0

原来我忘记在 ValidarMail 函数中设置 id 参数是可选的,所以在没有参数的情况下调用该函数时找不到该函数。

哦!

于 2014-10-10T15:30:19.430 回答