2

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; }
    }
}
Allfarid Morales García
  • 1,455
  • 2
  • 17
  • 27
  • What do you mean by *the user field*? There is no easy to way to answer this question as it stands, please include the model you are using for the form and the razor you are using to create the form. – Erik Philips May 02 '15 at 23:04
  • 2
    Properties that aren't bound in the form post will ALWAYS be null. Nothing you can do about that (except include hidden fields but doing that for fields you don't want the user modifying is dangerous...). When you submit a form, a *new* instance of the model is bound - the values in the original model that you populated are long gone. You need to fetch your data entity, update the appropriate fields and save it back again. – Ant P May 02 '15 at 23:23
  • 3
    This is where [View Models](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) become useful. The idea is to stop using your Entity model class in your views instead use a view model containing only the properties you are using in the particular view. Then submit the view model, retrieve an entity, and use the values from the view model to update your entity before you persist the changes. – Jasen May 02 '15 at 23:44
  • Go with @Jasen's suggestion, IMO. However, one note: you can pass all the fields to the form, and hide the ones you do not want to show in controls as hidden input fields. These will get posted back along with the user's edited items, and then you are not missing anything. However, from a design perspective, Jasen's suggestion, is much better. – DWright May 03 '15 at 01:02
  • I'd like adding hidden fields to the form, but it would be a security risk. And I cannot find a mvvm tutorial that works. Where did you learn that? – Allfarid Morales García May 03 '15 at 01:34
  • Here's an an [example](http://stackoverflow.com/a/29567728/2030565) to another question I posted earlier. There are lots of good tutorials if you search the web for "mvc view model tutorial". – Jasen May 03 '15 at 01:49

0 Answers0