前提・実現したいこと
Windows10(64ビット)、Visual Studio 2017 CommunityでASP.NET Core2.1で開発をしています。
(1)ファイル参照ボタンでファイルを読みたいです。
(2)ファイルの文字コードは複数あるのでラジオボタンで選択したいです。
画面に、ラジオボタンを出し、ボタンを押したときに、ラジオボタンの選択状態によって、挙動を変えたいと思っています。
具体的には、ボタンを押したときに、UTF8を選択していたら文字コードをUTF8に、Shift-JISのときはシフトJISにしたいです。
次のようにModelとView、Controllerを作りました。
読み取りボタンは動作して、ファイルを読めています。
ただ、コントローラーでReadTextFileメソッドに来たときに、TextReaderModelがコントローラーにないので、どうやってモデルのもっているラジオボタンの状態を取得すればよいのかがわかりません。
該当のソースコード
●Model
C#
1using System.Collections.Generic; 2 3namespace TextViewer.Model 4{ 5 public class TextReaderModel 6 { 7 public int OnTypeChanged { get; set; }//ここWebを見て書いてみたのですが、どう使うのか不明…。 8 public bool IsUtf8 { get; set; }//ここにboolを作ってみました。作ったのですが、どうViewに割り当てて、どうControllerで取得すればよいかわかりません。 9 public int IsShiftJis { get; set; }
●View
cshtml
1@using TextViewer.Model 2@using TextViewer.Resources 3 4@model TextReaderModel 5 6@section Scripts{ 7 <script> 8 JSCommon.replaceLiveEvent("#text_import_form", "submit", function () { 9 AjaxHelper.fileRequest({ 10 form: $('#text_import_form'), 11 iframe: $('#hidden_frame'), 12 success: function (data) { 13 window.alert("Success!!") 14 return false; 15 }, 16 error: function (data) { 17 window.alert("Error!!") 18 return false; 19 } 20 }); 21 }); 22 </script> 23} 24 25<form id="text_import_form" action="@Url.Action("ReadTextFile", "TextRead")" target="hidden_frame" method="post" class="" enctype="multipart/form-data"> 26 取込ファイル 27 <input id="upload_file" type="file" name="uploadFile" accept=".csv" /> 28 <div> 29 種別 30 <input asp-for="OnTypeChanged" type="radio" value="UTF8" />UTF8 31 <input asp-for="OnTypeChanged" type="radio" value="Shift-JIS" />Shift-JIS 32 </div>
●Controller
C#
1using CsvHelper; 2using Microsoft.AspNetCore.Http; 3using Microsoft.AspNetCore.Mvc; 4using System.IO; 5using System.Text; 6using System.Threading.Tasks; 7using Unity.Attributes; 8 9namespace TextViewer.Controllers 10{ 11 public class TextReaderController : CommonController 12 { 13 [EnableReferenceOnlyAction] 14 public async Task<IActionResult> Index(TextReaderModel model) 15 { 16 ViewData[CommonStrings.ViewDataKey.SearchCondition] = model; 17 return View(model); 18 } 19 20 [HttpPost] 21 public async Task<IActionResult> ReadTextFile(IFormFile uploadFile) 22 { 23 //ここでラジオボタンの値を取りたい。 24 25 26 var stream = uploadFile.OpenReadStream();
補足情報(FW/ツールのバージョンなど)
Windows10 64bit home
Visual Studio 2017 Community
ASP.NET Core 2.1