MVC는

2014-05-20 2 views
0

모델 컨트롤러에 뷰를 바인딩MVC는

public class ImportFiles 
{ 
    public string FileName; 
    public bool FileSelected { get; set; } 
} 

컨트롤러

파일 이름이오고, 모두가 내가 그런 단어 "직원"를 포함 (특정 폴더에서 파일을 얻기 위해 시도) 파일 이름에서 문자열을 검색하고 몇 가지 작업을 수행합니다. 이 프로그램에서

@model IList<ProjectName.Model.ImportFiles> 
@using AetnaCoventryMigration.Model; 

@using (Html.BeginForm("ImportFiles", "Admin", FormMethod.Post)) 
{ 
    <div class="panel panel-default"> 
    <table width="550px" class="mGrid table"> 
     <tr> 
     <th> 
      Select 
     </th> 
     <th> 
      File Name 
     </th> 
     </tr> 
     @for (var i = 0; i < Model.Count; i++) 
     { 
     <tr> 
      <td> 
       @Html.EditorFor(x => x[i].FileSelected) 
      </td> 
      <td> 
       @Model[i].FileName                
      </td> 
     </tr> 
     } 
    </table> 

[HttpGet] 
public ActionResult ImportFiles() 
{ 
    string folderpath = @"C:\Users\uvaish\Documents\Visual Studio 2010\Projects\MVCDemo\MVCDemo\Models"; 

    string filename = "*"; 
    string[] fileList = System.IO.Directory.GetFiles(folderpath, filename);//getting the file names from the folder as an array 

    List<ImportFiles> inputFiles = new List<ImportFiles>(fileList.Length);//making a list of same number of elements as the number of files    

    foreach (string str in fileList) 
    { 
     ImportFiles inputFile = new ImportFiles(); 
     inputFile.FileName = Path.GetFileName(str); 
     inputFile.FileSelected = false; 
     inputFiles.Add(inputFile); 
    } 
    return View(inputFiles); 
} 


[HttpPost] 
public string ImportFiles(List<ImportFiles> import) 
{ 
    foreach (ImportFiles importFile in import) 
    { 
     if (importFile.FileSelected == true) 
     { 
      if (importFile.FileName.Contains("ployee"))//Getting a null point reference here 
      { 
       return ("file found"); 
      } 
      else 
      { 
       return ("no file found"); 
      } 
     } 
     else 
     { 
      return ("no file selected"); 
     } 
    } 
    return ("done"); 
}  

보기, 내가보기에 전달 된 각 개체에 대해 확인란뿐만 아니라 파일 이름에 접근하려합니다. 확인란에 액세스 할 수 있으며 파일이 true 또는 false (checked 또는 unchecked)인지 여부는 알지만 파일 이름에는 액세스 할 수 없습니다.

답변

1

로 변경

<td> 
     @Model[i].FileName                
    </td> 

:

@Html.DisplayFor(x => x[i].FileName) 
    or add 
    @Html.HiddenFor(x => x[i].FileName) 

올바른 데이터가 근무

+0

을 바인딩 필드를 생성 할 필요도 좀 더 problems.Thanks을했다! :) –

+0

+1 OP가 당신의 대답이 그를 위해 일 했으므로 그러나 이상하게 upvote하지 않았다. –

+0

슬픔 만 느낄 수 있습니다. –