iT邦幫忙

0

ASP.net Index.cshtml 與 Index.cshtml.cs傳值問題 及動態生成問題

目前測試 在cs上面

public int C { get; set; }
public List<string> ListInfor;

public void OnGet()
{
    C = 6;
    ListInfor = getList();
}

public List<string> getList()
{
  List<string> myStringLists = new List<string>();
  myStringLists.Add("大家好!1");
  myStringLists.Add("大家好!2");
  myStringLists.Add("大家好!3");
  return myStringLists;
}

cshtml上面

@{
    @Model.C
     List<string> c = Model.ListInfor;
}

@foreach (var item in c) {
    //顯示大家好
    <p>@item</p>
}

這樣可以把值從cshtml.cs傳到cshtml

想請問如何把值在從cshtml 傳到cshtml.cs

請問如何動態生成文字框 及內部變數控制

<form method="post" name="" action="">
        <p>名稱</p> <input name="name" value="123">
        
</form>
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
石頭
iT邦高手 1 級 ‧ 2018-01-12 21:33:49

你是寫.net MVC吧?

你要把你所用到的資料附給 Model 變數
而且你Action那邊寫法也怪怪的

Index.cshtml.cs 程式碼

public ActionResult OnGet()
{
    TestViewModel viewModel = new TestViewModel()
    {
        C = 6,
        ListInfor = getList()
    };
    return View(viewModel);
}

public class TestViewModel
{
    public List<string> ListFor { get; set; }

    public int C { get; set; }
}

Index.cshtml 程式碼

@model TestViewModel

@foreach (var item in Model.ListInfor) {
    //顯示大家好
    <p>@item</p>
}

你可能要花點時間看看 微軟的快速入門文檔..

我們透過路由呼叫Asp.net連結 會在動態幫我們編譯View的Html代碼
其中資料最常使用Model變數來傳輸

小魚 iT邦大師 1 級 ‧ 2018-01-12 23:00:31 檢舉

MVC 有 Index.cshtml.cs 嗎??
前端傳到後端基本上要用Get或Post吧...

石頭 iT邦高手 1 級 ‧ 2018-01-13 23:23:18 檢舉

提問者問題 不清楚
我只能依照我猜測的幫忙回答

makstfate iT邦新手 5 級 ‧ 2018-01-14 01:32:23 檢舉


請問紅色框內的兩個文檔 如何互傳
cshtml.cs傳6給cshtml目前是會顯示的
但是不知道如何從cshtml呼叫與傳送數據給cshtml.cs

我要發表回答

立即登入回答