0

My models has some fields that are not to be presented in views (like Id field).

So, when I post the form, these fields return with "null" value, unless I insert then as hidden fields in form.

There are another away to update a model, using only the fields in form ?

My actual code:

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Profissao model)
{
    if (ModelState.IsValid)
    {
        using (var escopo = Db.Database.BeginTransaction())
        {
            try
            {
                if (model.Id == 0)
                    Db.Profissoes.Add(model);
                else
                    Db.Profissoes.Update(model);

                Db.SaveChanges();
                escopo.Commit();

                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                escopo.Rollback();
            }
        }                
    }

    return View(model);
}
6
  • Can you reconstruct them server-side? Re-retrieve from the db, keep them in session, ...? Commented Mar 9, 2016 at 9:01
  • 2
    you should make use of ViewModels, otherwise you will have to expose your DB entity structure right to the View - hidden fields, ViewData and other potentially dangerous structures. And you should use this ViewModel to map changes made by user to your DB entity. Commented Mar 9, 2016 at 9:02
  • There are ways to associate the data being sent from the client with the desired entity. The question is, why not rely on the Id? It's usually the easiest and safest bet... Commented Mar 9, 2016 at 9:02
  • And don't forget to encrypt decrypt id or keep it in guid format in order to prevent Id modified by client side Commented Mar 9, 2016 at 9:07
  • @raderick, this will be filling field by field? Commented Mar 9, 2016 at 9:08

1 Answer 1

2

You should use Dto's (Data transfer objects) to handle this.

public class User
{
    public string Name { get; set; }

    public string Passord { get; set; }

    public  string Email { get; set; }
}


public class UserDto
{
    public string Name { get; set; }

    public string Passord { get; set; }

    public string Email { get; set; }

    public UserDto FromModel(User user)
    {
        Name = user.Name;
        Passord = user.Passord;
        Email = user.Email;
        return this;
    }

    public User UpdataModel(User user)
    {
        user.Name = Name;
        user.Email = Email;

        return user;
    }
}

then you can pass around the Dto object to your view and in your post.

your post controller should look somthing like

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(ProfissaoDto model)
{
    if (ModelState.IsValid)
    {
        using (var escopo = Db.Database.BeginTransaction())
        {
            try
            {
                if (model.Id == 0)
                    Db.Profissoes.Add(ProfissaoDto.UpdateModel(new Profissao()));
                else
var model = Db.Profissao.find(Model.id);

                    Db.Profissoes.Update(ProfissaoDto.UpdateModel(model));

                escopo.Commit();
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                escopo.Rollback();
            }

        }                
    }
    return View(model);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.