색인이 제대로 사용되는 한이 문제가되지 않습니다. 여기에 양식 이름을 구상하는 방법이 나와 있습니다.
모델 [0]
.bar 인
모델 [0] .Inner [0] 모델 [0] .Inner [1]
.bar 인 외측 모델 속성을 갖는 경우
을 .foo 외부 모델에는 내부 객체 컬렉션 인 inner라는 속성이 있습니다. 내부 개체에는 bar라는 속성이 있습니다. 양식이 올바른 색인으로 렌더링되면 모델 바인딩이 작동해야합니다. 양식 필드가 클라이언트 측에서 생성되는 경우 상황이 까다로워 질 수 있습니다. 모델을 조작하기 위해 서버로 돌아가는 것이 좋습니다. 추가 왕복 여행이 있지만 Ajax 요청을 통해 여행을 할 수 있습니다.
다음은 좀 더 자세한 예입니다.여기
public class InnerModel{
public string Name{get; set;}
}
public class OuterModel{
public List<InnerModel> InnerModels{get; set;}
public string Name{get; set;}
}
내가 같을 것이다 내 시야를 구상 할 것입니다 : 이것은 형태로 포장되어있는 경우
@model IEnumerable<OuterModel>
<ul>
@{int i = 0;}
@foreach(var item in Model){
<li>
Outer Name : @Html.TextBoxFor(m=>Model[i].Name)
<br />
@{int j = 0;}
<ul>
@foreach(var innerItem in Model[i].InnerModels){
<li>Inner Name : @Html.TextBoxFor(m=> Model[i].InnerModels[j].Name)</li>
j++;
}
</ul>
i++;
</li>
}
</ul>
--- 및 컨트롤러 액션은 다음과 같습니다
public ActionResult Action(List<OuterModel> model){
}
그러면 모델이 올바르게 채워질 것이라고 생각합니다.
나는 당신의 모양을 알아 챘다. 나에게 보이지 않는다. 나는 OuterModel을 통과하는 것이 효과가있을 것이라고 생각하지 않는다. 솔직히 내가 틀렸지 만. 여기
@using (Html.BeginForm("Create", "Controller", new { OuterModels = Model }, FormMethod.Post))
{
//Code to create the InnerModels here
}
은 .. 내가 가르치는 클래스 .. 것을 확실히 작동에 대한 한 예입니다
public class Author
{
public string Name { get; set; }
}
public class Book
{
public string Name { get; set; }
public List<Author> Authors { get; set; }
}
컨트롤러 :
public class BookController : Controller
{
public static List<Book> _model = null;
public List<Book> Model
{
get
{
if (_model == null)
{
_model = new List<Book>
{
new Book{
Name = "Go Dog Go",
Authors = new List<Author>{
new Author{Name = "Dr. Seuss"}
}},
new Book{
Name = "All the Presidents Men",
Authors = new List<Author>{
new Author{Name = "Woodward"},
new Author{Name = "Bernstein"}
}},
new Book{
Name = "Pro ASP.NET MVC Framework",
Authors = new List<Author>{
new Author{Name = "Sanderson"},
new Author{Name = "Stewart"},
new Author {Name = "Freeman"}
}}
};
}
return _model;
}
}
public ActionResult Index()
{
return View(Model);
}
public ActionResult Edit()
{
return View(Model);
}
[HttpPost]
public ActionResult Edit(List<Book> books)
{
_model = books;
return RedirectToAction("Index");
//return View(books);
}
}
및 전망 :
@model List<AmazonWeb.Models.Book>
@{
ViewBag.Title = "Index";
}
<div class="content">
@Html.ActionLink("Index", "Index")
@using (Html.BeginForm())
{
<input type="submit" value="save" />
<ul class="book-list">
@for (var i = 0; i < Model.Count; i++)
{
<li>
<label>Book Name</label> : @Html.TextBoxFor(m => Model[i].Name)
<ul>
@for (var j = 0; j < Model[i].Authors.Count; j++)
{
<li><label>Author Name</label> : @Html.TextBoxFor(m => Model[i].Authors[j].Name)</li>
}
</ul>
</li>
}
</ul>
<input type="submit" value="save" />
}
</div>
여기에 innermodels를 만들려면 * 코드를 게시해야합니다. * – VJAI
코드의 특정 부분이 실제 문제와 독립적이므로 내 코드를 게시하는 것을 주저합니다. 내 문제는 모델을 컨트롤러에 전달하는 것이지 InnerModels을 만드는 것이 아닙니다. 이것은 단순한 예일뿐입니다. –