2017-02-01 33 views
0

我從控制器傳遞變量以使用ViewBag查看。控制器部分:如何將變量分配給按鈕asp.net mvc 5

ViewBag.Counter = count; 

在我看來,我做一個按鈕,我想按鈕值賦給這個變量,每次按下按鈕時,你得到的變量。這是我的代碼中的觀點:

@using (Html.BeginForm("UploadExcel", "Probe", FormMethod.Post, new { enctype = "multipart/form-data", onsubmit = "return myFunction()" })) 
{ 
<div class="form-horizontal"> 
    <div class="form-group"> 
     <div class="col-md-10"> 
      <input type="file" id="FileUpload" name="FileUpload" class="" /> 
     </div> 
    </div> 
    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <br /> 
      <input class="inner" type="submit" value="Upload" id="btnSubmit" /> 
      <button class="inners" value="@ViewBag.Counter">But</button>     
      <button>@Html.ActionLink("View Domain Name", "Domain", "Probe") </button> 
     </div> 
    </div> 
</div> 
} 

當我按下它給了我一些奇怪的行按鈕: 「\ u003cul \ u003e」, 「\ u003cli \ u003ePlease選擇Excel文件\ u003c /李\ u003e」 「\ u003c/ul \ u00ee」] ,我在我的地址欄中看到它重定向到http://localhost:54570/Probe/UploadExcel它應該向我們顯示變量值。

+0

你的意思_doesn't爲我工作?不用括號也可以試試'@ ViewBag.Counter',並確保'count'不爲空或爲空 –

+0

我編輯問題。 – Vytautas

+0

也添加您的操作方法'UploadExcel'。你只想調用你的方法並傳遞文件和計數器值? –

回答

0

<button class="inners" value="@ViewBag.Counter">@ViewBag.Counter</button>

0

我給你點M.Wiśnicki。我創建了新的視圖,然後將操作方法​​添加到控制器並將計數器傳遞給該視圖。按鈕我有我的主視圖「索引」所以我只是簡單地調用方法時按下按鈕

<a href="@Url.Content("~/Probe/TotalEntries")"> <input type="button"value="Total Records" /> </a>

其重定向到視圖,並顯示(插入的行數)的計數器。