如何将模型绑定到视图以显示inout文本框中的数据。

时间:2016-05-19 14:48:32

标签: c# asp.net-mvc asp.net-mvc-4 razor

我刚刚开始使用MVC4 razor视图。截至目前我已经完成了保存和记录列表。现在我正在尝试编辑记录。但我无法将模型对象中的数据从控制器绑定到视图输入元素。在这种情况下,有人可以帮助我。?

查看:

@using (Html.BeginForm("PersonAction", "Admin"))
{
  <input type="submit" name="SubmitAction" value="New" id="btnNew" tabindex="65" title="New" class="btnInner-Save btnInner-size" />

  <input type="submit" name="SubmitAction" value="Edit" id="btnEdit" tabindex="65" title="Edit" class="btnInner-Save btnInner-size" />
  <div class="div2col-S">
    <label id="lblFirstName" for="Name1">First Name</label>
    @Html.TextBoxFor(m => m.Name1) </div> <div class="div2col-S">
    <label id="lblSecondName" for="Name2">Second Name</label>
    @Html.TextBoxFor(m => m.Name2)
  </div>
}

模型

public class person
{
  public virtual string Name1
  {
    get { return this._name1; }
    set { this._name1 = value; }
  }

  public virtual string Name2
  {
    get { return this._name2; }
    set { this._name2 = value; }
  }
}

控制器

public class AdminController : Controller
{
    [HttpGet]
    public ActionResult Person()
    {
        var persongrdList = new PersonBusiness().GetList();
        ViewBag.PersonGridList = persongrdList;
        return View();
    }

    [HttpPost]
    public ActionResult PersonAction(string SubmitAction, Person mdl)
    {
        switch (SubmitAction)
        {
            case Constants.NEW_ACTION:
                break;
            case Constants.EDIT_ACTION:
                Person p = new PersonBusiness().GetByPrimaryKey(mdl.PersonID);
                ViewBag.SelectedRecord = p;//Need help over here. How to bind this person model data to view input elements?
                break;
            case Constants.SAVE_ACTION:
                bool success = new PersonBusiness().Insert(mdl, 1);

                break;
            case Constants.CANCEL_ACTION:
                break;

        }
        return RedirectToAction("Person", "Admin");

    }
}

2 个答案:

答案 0 :(得分:1)

你需要像这样返回你的模型:

var personDetails = new PersonDetails()
var p = new PersonBusiness().GetByPrimaryKey(mdl.PersonID);

personDetails.Name1 = p.Name1;
personDetails.Name2 = p.Name2;

return View("ViewName", new SmartClinic.Models.Person { PersonDetails = personDetails });

在视图中返回后:

@model SmartClinic.Models.PersonDetails
@Html.TextBoxFor(m => m.Name1)
@Html.TextBoxFor(m => m.Name2)

更新

public class Person
{
   public PersonDetails PersonDetails { get; set; }
}

public class PersonDetails 
{
     public string Name1 { get; set; }
     public string Name2 { get; set; }
}

答案 1 :(得分:1)

我认为你应该有一些改变:

型号:

public class Person
{
    public int Id { get; set; }
    public string Name1 { get; set; }
    public string Name2 { get; set; }
}

您应该创建PersonController并使用索引操作列出人员:

public ActionResult Index()
{
    List<Person> people = new List<Person>();
    return View(people);
}

GetMethod制作模型(人物)并将其发送到视图:

public ActionResult Edit(int id)
{
    Person p = new PersonBusiness().GetByPrimaryKey(id);
    return View(p);
}

PostMethod编辑发布到操作的模型:

[HttpPost]
public ActionResult Edit(Person p)
{
    if (ModelState.IsValid)
    {
        var peopleToEdit = new Person
        {
            Name1 = p.Name1,
            Name2 = p.Name2
        };
        //Commit your change
        return RedirectToAction("Index");
    }
    return View("Edit", p);
}

索引视图:

@model IEnumerable<Person>
//List your persons and a button to edit for every person

编辑视图:

@model Person
@using (Html.BeginForm("Edit", "Person", FormMethod.Post))
{
    @Html.TextBoxFor(m => m.Name1)
    @Html.TextBoxFor(m => m.Name2)
    //...
}