-1

[HttpGet] ActionResult Edit

Model model = dbContext.Table.Find(id);
ViewBag.MyID = new SelectList(dbContext.Table, "id", "value", model.value);

View Edit

@Html.DropDownList("MyID", (IEnumerable<SelectListItem>)ViewBag.MyID)

[HttpPost] ActionResult Edit

public ActionResul Edit(..., string MyID)

My Model

[Table("tb_estados_uf")]
public class Estado
{
    [Key]
    public int uf_id { get; set; }

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

    [Display(Name = "UF")]
    public string uf_sigla { get; set; }

    public IEnumerable<Estado> ufIE { get; set; }
}

This is the top of my view:

@model Oficial.Models.Candidato

My Controller

public ActionResult Edit([Bind(Include ="(Candidato's Columns)..., uf_id (FK)")] Candidato objCandidato, FormCollection fc, HttpPostedFileBase file, string MyID)

The problem is this parameter, he's always 1

even when its recorded with the last record of my database.

Have anything that am i forget?

sorry with my english

Leonardo
  • 1
  • 4

1 Answers1

0

Your models should be free of presentation attributes

[Table("tb_estados_uf")]
public sealed class Estado
{
    [Key]
    public int uf_id { get; set; }

    public string uf_estado { get; set; }

    public string uf_sigla { get; set; }

    public IEnumerable<Estado> ufIE { get; set; }
}

Then you create a separate view model for the purpose of collecting data from the user.

public sealed class EstadoViewModel
{
  public int uf_id { get; set; }

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

  [Display(Name = "UF")]
  public string uf_sigla { get; set; }

  public IEnumerable<Estado> ufIE { get; set; }
}

Your new view model:

public sealed class Candidato
{
  public int Id { get; set; }
  public string Phone { get; set; }
  public string OtherField { get; set; }
  public EstadoViewModel EstadoVM { get; set; }
}

Then on top of your View:

@model Oficial.Models.Candidato

Then add the drop down control in your view:

@Html.DropDownListFor(m => m.EstadoVM.uf_sigla, Model.EstadoVM.ufIE, new { @class = "form-control input-sm" })

Finally in your controller:

[HttpPost]
public ActionResul Edit(Candidato model) 
{
  // Code action here
}
Keith Harris
  • 1,118
  • 3
  • 13
  • 25
  • I'll edit my post with some others things – Leonardo Aug 07 '18 at 22:02
  • I tried with DropDownListFor and its work for bring others id (MyID), but I have an Error with this: dbContext.Entry(objCandidato).State = EntityState.Modified; dbContext.SaveChanges(); <<<<------ This line – Leonardo Aug 07 '18 at 22:33
  • My view is create an person and i need to select where he from like: Name, Telephone blabla, and this dropdownlist is for state. because of that, my top of my model is different from my other model 'Estado' Am I being clear? Sorry if this sounds ignorant, but I'm trying to be polite. – Leonardo Aug 07 '18 at 22:39
  • Yes, you can only use one model per view. For that reason you must add another view model to your model. I'll edit my answer. – Keith Harris Aug 07 '18 at 22:41
  • now, i dont have access to my database, only tomorrow, but I'm grateful already, and tomorrow i say if its work :) – Leonardo Aug 07 '18 at 22:43
  • i got it, My Solution: Using files that i post first time here, i modified my candidato model, add this line public IEnumerable EstadoList { get; set; } My view: @Html.DropDownListFor(m => m.uf_id, Model.EstadoList, new { @class = "form-control"}) My Edit HttpGet: candidato.EstadoList = new SelectList(dbContext.Estados, "uf_id", "uf_estado"); My Edit HttpPost: get uf_id with the rest of mt 'candidato model' parameter THANKS SO MUCH, even that i didn't use your solution, you help me to think better – Leonardo Aug 08 '18 at 13:08