I am trying to make an edit form for certain user data, but every field that is not in the form is set to null.
This is my controller function so far. I have a "user" field that is not used in the form and is always set to 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);
}
I already tried with Bind(Include = "")
, with Bind(Exclude = "")
and UpdateModel(emisor, new[] {"rfc", "nombre_comercial"});
but nothing works, the user field will be always set to null.
Would you please help me?
Edit: this is my model. Thank you
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; }
}
}