Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将模型绑定到视图以在inout文本框中显示数据。?_C#_Asp.net Mvc_Asp.net Mvc 4_Razor - Fatal编程技术网

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

C# 如何将模型绑定到视图以在inout文本框中显示数据。?,c#,asp.net-mvc,asp.net-mvc-4,razor,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=

我刚刚开始使用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 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");

    }
}

您需要按如下方式返回您的模型:

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; }
}

我想你应该换些零钱:

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

public ActionResult Index()
{
    List<Person> people = new List<Person>();
    return View(people);
}
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);
}
索引视图:
您的编辑操作在哪里?很抱歉,我刚刚添加了输入元素,该代码已经存在于我的视图的开头@{Layout=null;}@model SmartClinic.Models.persono返回列表,执行我答案中的第一个片段,然后m=>m.Person[0]。Name会将您的值输入文本框,这就是问题所在,对吗?没有将值绑定到文本框?上面编辑的是您的目标吗?只需将p对象返回到视图。我刚才尝试了以下方法:返回视图(“Person”,(SmartClinic.Models.Person)p)。仍然不工作,但在我看来它仍然像->@Html.TextBoxFor(m=>m.Name1)
public ActionResult Index()
{
    List<Person> people = new List<Person>();
    return View(people);
}
public ActionResult Edit(int id)
{
    Person p = new PersonBusiness().GetByPrimaryKey(id);
    return View(p);
}
[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)
    //...
}