2017-04-19 462 views
3

私は作業中のアップロードファイル機能のファイル名を作成するために、ビューからデータを送信してコントローラ内で使用しようとしています。ViewからControllerへのモデルデータの受け渡しとその値の使用

コントローラ

// GET: File 
    [Authorize(Roles = "Admin, Lecturer")] 
    public ActionResult Index() 
    { 



     foreach (string upload in Request.Files) 
     { 
      if (Request.Files[upload].FileName != "") 
      { 
       string path = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/uploads/"; 
       string filename = Path.GetFileName(Request.Files[upload].FileName); 
       Request.Files[upload].SaveAs(Path.Combine(path, filename)); 
      } 
     } 
     return View(); 
    } 

モデル

public class UploadModel 
{ 
    [Required(ErrorMessage = "Course is required")] 
    public string Course { get; set; } 
    [Required(ErrorMessage = "Title is required")] 
    public string Title { get; set; } 

    public string Uploader { get; set; } 
} 

ビュー

<div class="uploadContainer"> 
    <table> 


     <tr> 
      <td>Title :</td> 
      <td colspan="2" class="editPostTitle"> 
       @Html.TextBoxFor(tuple => tuple.Item1.Title, new { @class = "uploadTitleInp" }) 
       @Html.ValidationMessageFor(tuple => tuple.Item1.Title) 
      </td> 
     </tr> 

     <tr> 
      <td>Course :</td> 
      <td> 
       @{ 
        List<SelectListItem> listItems = new List<SelectListItem>(); 
        foreach (var cat in courses) 
        { 
         listItems.Add(new SelectListItem 
         { 
          Text = cat.Course.Name, 
          Value = cat.Course.Name 
         }); 
        } 
       } 

       @Html.DropDownListFor(tuple => tuple.Item1.Course, listItems, "-- Select Status --") 

       @Html.ValidationMessageFor(tuple => tuple.Item1.Course) 
      </td> 
     </tr> 

     <tr> 
      <td>File :</td> 
      <td> 
       <input type="file" name="FileUpload1" id="fileUpload" required /> 
      </td> 
     </tr> 

     <tr> 
      <td></td> 
      <td> 
       <input id="btnUploadFile" type="button" value="Upload File" /> 
      </td> 
     </tr> 

    </table> 

</div> 

このメソッドは、PLACのために責任がありますディレクトリにアップロードされたファイルを削除します。私ができるようにしたいのは、このようなことをしてファイル名を作成することです。

string filename = model.Title + " - " + model.Course; 

私は通常、データを格納するDBを使用する際にこれを達成する方法を知っているが、私はデシベルでアップロードされたファイルを格納しておりませんので、その後、私は本当ににモデルデータを渡す方法がわかりません私はユーザーがファイル名を構成するために入力した値を使用することができます。私はこのフレームワークと言語に対して比較的新しいので、どんな助けと指針も大きく訴えるでしょう。

ありがとうございます!

+0

チェックこのアウト:http://stackoverflow.com/questions/16339398/mvc4-passing-model-from-view-to-controller それは – Javier

答えて

0

モデルを通して別のコントローラメソッドにデータを戻す必要があります。これは次のように実装することができます(私はあなたのコードビットに簡略化しましたが、一般的な実装が動作するはずです):

public class UploadViewModel 
{  
    public string Course { get; set; } 
    public string Title { get; set; } 
} 

あなたのGETの処置:

public ActionResult Index() 
{ 
    return View(new UploadViewModel()); 
} 

は、その後、あなたのビューにモデルを追加し、フォームで使用すると、データがモデルにバインドされます。これをコントローラに送り返すことができます。

@model UploadViewModel 
@using(Html.BeginForm()) 
{ 
    Course: @Html.TextBoxFor(s=>s.Course) 
    Title: @Html.TextBoxFor(s=>s.Title) 
    <input type="submit" value="Save file" /> 
} 

は、今すぐあなたのコントローラでHttpPostアクションメソッドを通じて値を取得:データ controllerを送信するには、2つの異なる方法があります

[HttpPost] 
public ActionResult Index(UploadViewModel model) 
{ 
    //You can use model.Course and model.Title values now 
} 
+0

おかげで、あなたは合格の問題を解決した私を助けましたデータ。しかし、私はまだカスタムファイル名でアップロードされているファイルをどのように保存するのか分かりませんが、それはまったく別の質問ですね! – Plumbus

+0

@Plumbus聞いてよかった!私は今問題を解決するのに十分な情報がありません。確かに、私が考えていることのために別の質問を書くのが最善でしょう。 – Jurjen

0

javascriptの上で転送オブジェクトを作成されます:あなたはAjaxのポストメソッドを使用してController method

に送るデータと一致する必要があります。オブジェクトプロパティ名 は、Modelプロパティ名と同じである必要があります。FormPost方法

Html.BeginFomを使用して

var objAjax = new Object(); 
    objAjax.Course = 'newCourse'; // Model prop is string type so this value must be string. 
    objAjax.Title = 'newTitle'; 

    $.ajax('@Url.Action("MethodName", "ControllerName")', { 
       type: 'POST', 
       data: JSON.stringify(objAjax), 
       contentType: "application/json", 
       datatype: "json", 
       traditional: true, 
       success: function (returnData) { 
        // Do something when get success 
       }, 
       error: function() { 
        // Do something when get an error 
       } 
      }); 



    [HttpPost] 
    public ActionResult Index(UploadViewModel model) 
    { 
     //do someting 
    } 

とき 押して送信ボタン

コントローラにモデル内のすべてのデータを送信ForExample

@using(Html.BeginForm()) 
{ 
    <div> 
     // Your code 

     <div> 
      <input type="submit" value="Go" /> 
     </div> 
    </div> 
} 


[HttpPost] 
public ActionResult Index(UploadViewModel model) 
{ 
    //do someting 
} 
関連する問題