POST方法不适用于MVC 4

时间:2014-05-23 00:23:01

标签: asp.net-mvc asp.net-mvc-4 post

我尝试在我的桌面艺术中创建一篇新文章,但POST方法不起作用,我现在不知道为什么,编辑文章工作得很好,我读了许多帖子形式的主题,没有,我希望任何人都可以帮助我

型号

public class Art
{        
    [Key]
    public int idArt { get; set; }
    [DisplayName("Codigo Artículo")]
    [Required(ErrorMessage = "Codigo Artículo Requerido")]
    [MaxLength(30)]
    public string co_art { get; set; }

    [DisplayName("Tipo Articulo")]
    [ForeignKey("TypeArticles")]
    [Required(ErrorMessage = "Tipo Artículo Requerido")]
    public int IDType { get; set; }
    public virtual TypeArticles TypeArticles { get; set; }

    [DisplayName("Descripción")]
    [Required(ErrorMessage = "Descripción Artículo Requerido")]
    [MaxLength(150)]
    public string des_art { get; set; }
    [DisplayName("Modelo")]
    [Required(ErrorMessage = "Modelo Artículo Requerido")]
    [MaxLength(50)]
    public string modelo { get; set; }
    [DisplayName("Referencia")]
    [MaxLength(50)]
    [Required(ErrorMessage = "Referencia Artículo Requerido")]
    public string referencia { get; set; }
    [DisplayName("Linea Artículo")]
    [ForeignKey("Linea")]
    [Required(ErrorMessage = "Linea Artículo Requerido")]
    public int IdLinea { get; set; }
    public virtual Linea Linea { get; set; }
    [DisplayName("Categoria Artículo")]
    [ForeignKey("Categoria")]
    [Required(ErrorMessage = "Categoria Artículo Requerido")]
    public int idCat { get; set; }
    public virtual Categoria Categoria { get; set; }        

    [DisplayName("Precio Venta")]
    [Range(0.01, 999999999, ErrorMessage = "Precio debe estar entre 0.01 y 999999999")]
    public double Price { get; set; }

    [MaxLength(1024)]
    [DisplayName("Info. Adicional")]
    public string Adicional { get; set; }

    [MaxLength(100)]
    public string Photo { get; set; }
}

控制器POST方法

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Create(Art artmodels)
    {
        ViewBag.idLinea = new SelectList(db.Linea.OrderBy(c => c.des_lin), "IdLinea", "des_lin");
        ViewBag.IdCat = new SelectList(db.Categorias.OrderBy(c => c.des_cat), "IdCat", "des_cat");
        ViewBag.IDType = new SelectList(db.TypeArticles.OrderBy(c => c.TypeDesc), "IDType", "TypeDesc");

        if (ModelState.IsValid)
        {
            var art_exists = (from inv in db.Art where inv.co_art == artmodels.co_art.Trim() select inv).FirstOrDefault();
            if (art_exists != null)
            {
                ModelState.AddModelError("co_art", "Codigo de Articulo ya Existe");
                return View(artmodels);
            }

            db.Art.Add(artmodels);
            db.SaveChanges();
            ///
            //int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            //var articulos = db.Art;

            //IPagedList<Art> art_paged = null;
            //art_paged = articulos.OrderBy(i => i.co_art).ToPagedList(currentPageIndex, (pagesize.HasValue) ? pagesize.Value : defaultPageSize);

            return RedirectToAction("Edit", "Articulos", new {id = artmodels.idArt });                
        }       

        this.Response.StatusCode = 400;
        return View(artmodels);
    }

查看

@model mvcAmerica.Models.Art
@{
    ViewBag.Title = "Creacion";
}

<h1><small>Creación Articulos</small></h1>

@using (Html.BeginForm("Create", "Articulos", FormMethod.Post))
{
    @Html.ValidationSummary(true)
    <text>
        @{Html.RenderPartial("CreateOrEditArticulos", Model);}
    </text>
}

的RenderPartial

@model mvcAmerica.Models.Art


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
<fieldset>
    @Html.HiddenFor(model => model.idArt)

        <div class="clearfix">
            @Html.LabelFor(model => model.co_art)

            <div class="input">
                @Html.EditorFor(model => model.co_art)
                @Html.ValidationMessageFor(model => model.co_art)
            </div>
        </div>

    <div class="clearfix">
        @Html.LabelFor(model => model.des_art)

        <div class="input">
            @Html.EditorFor(model => model.des_art)
            @Html.ValidationMessageFor(model => model.des_art)
        </div>
    </div>

    <div class="clearfix">
        @Html.LabelFor(model => model.IDType, "Tipo Articulo")

        <div class="input chosen-select">
            @Html.DropDownList("IDType", String.Empty)
            @Html.ValidationMessageFor(model => model.IDType)
        </div>
    </div>

    <div class="clearfix">
        @Html.LabelFor(model => model.modelo)

        <div class="input">
            @Html.EditorFor(model => model.modelo)
            @Html.ValidationMessageFor(model => model.modelo)
        </div>
    </div>

    <div class="clearfix">
        @Html.LabelFor(model => model.referencia)

        <div class="input">
            @Html.EditorFor(model => model.referencia)
            @Html.ValidationMessageFor(model => model.referencia)
        </div>
    </div>

    <div class="clearfix">
        @Html.LabelFor(model => model.IdLinea)

        <div class="input chosen-select">
            @Html.DropDownList("IdLinea", String.Empty)
            @Html.ValidationMessageFor(model => model.IdLinea)
        </div>
    </div>

    <div class="clearfix">
        @Html.LabelFor(model => model.idCat)

        <div class="input chosen-select">
            @Html.DropDownList("IdCat", String.Empty)
            @Html.ValidationMessageFor(model => model.idCat)
        </div>
    </div>

    <div class="clearfix">
        @Html.LabelFor(model => model.Price)
        <div class="input">
            @Html.EditorFor(model => model.Price)
            @Html.ValidationMessageFor(model => model.Price)
        </div>
    </div>


    <div class="clearfix">
        @Html.LabelFor(model => model.Adicional)

        <div class="input">
            @Html.EditorFor(model => model.Adicional)
            @Html.ValidationMessageFor(model => model.Adicional)
        </div>
    </div>

    <div class="actions">
        <input type="submit" class="btn primary" value="Guardar" />
        @Html.ActionLink("Listado", "Index", null, new { @class = "btn" })
    </div>
</fieldset>
}

感谢帮助我解决这个问题...

1 个答案:

答案 0 :(得分:1)

您的代码中嵌套了导致此问题的表单。提交按钮位于内部,但它没有任何Controller和Anction方法可以调用,因此它不会将数据发布到任何方法。

所以你需要改变这样的代码:

查看

@model mvcAmerica.Models.Art
@{
    ViewBag.Title = "Creacion";
}

<h1><small>Creación Articulos</small></h1>

//the commented line should go to the partial view
//@using (Html.BeginForm("Create", "Articulos", FormMethod.Post))
//{
//    @Html.ValidationSummary(true)
    <text>
        @{Html.RenderPartial("CreateOrEditArticulos", Model);}
    </text>

<强>的RenderPartial

@model mvcAmerica.Models.Art

@using (Html.BeginForm("Create", "Articulos", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

   // rest of the code is as it is
}