如何从asp.net mvc中的编辑功能中排除字段?

时间:2015-05-02 22:58:48

标签: c# asp.net-mvc model-binding

我正在尝试为某些用户数据创建一个编辑表单,但是表单中没有的每个字段都设置为null。

到目前为止,这是我的控制器功能。我有一个"用户"表单中未使用的字段,始终设置为null。

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "EmisorId,razon_social,rfc,nombre_comercial,calle,no_ext,no_int,colonia,localidad,referencia,municipio,estado,pais,cp")] Emisor emisor)
{
    if (ModelState.IsValid)
    {
        db.Entry(emisor).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(emisor);
}

我已尝试使用Bind(Include = "")Bind(Exclude = "")UpdateModel(emisor, new[] {"rfc", "nombre_comercial"});,但无效,用户字段将始终设置为空。

你能帮帮我吗?

编辑:这是我的模特。谢谢

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace Facturacion.Models
{
    public class Emisor
    {
        public int EmisorId { get; set; }

        [Required]
        [Display(Name = "Razón social")]
        public string razon_social { get; set;}

        [Required]
        [Display(Name = "RFC")]
        [RegularExpression("[A-Z,Ñ,&]{3,4}[0-9]{2}[0-1][0-9][0-3][0-9][A-Z,0-9]?[A-Z,0-9]?[0-9,A-Z]?", ErrorMessage = "El RFC es inválido")]
        public string rfc { get; set;}

        [Display(Name = "Nombre comercial")]
        public string nombre_comercial { get; set;}

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

        [Display(Name = "Número exterior")]
        public string no_ext { get; set;}

        [Display(Name = "Número interior")]
        public string no_int { get; set;}

        [Display(Name = "Colonia")]
        public virtual string colonia { get; set;}

        [Display(Name = "Localidad")]
        public string localidad { get; set;}

        [Display(Name = "Referencia")]
        public string referencia { get; set;}

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

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

        [Required]
        [Display(Name = "País")]
        public string pais { get; set;}

        [Required]
        [RegularExpression("[0-9][0-9][0-9][0-9][0-9]", ErrorMessage = "El código postal es inválido")]
        [Display(Name = "Código postal")]
        public string cp { get; set;}

        [Display(Name = "Activo")]
        public bool activo { get; set;}

        public string user { get; set; }
    }
}

0 个答案:

没有答案