【问题标题】:Pass value in asp.net mvc在 asp.net mvc 中传递值
【发布时间】:2017-04-19 05:42:40
【问题描述】:

我正在尝试将方法的数据传递给另一个方法。你能帮帮我吗?

这是我的代码:return View(model); 模型应该通过 public ActionResult ShowData(Student model) 方法。

public ActionResult ShowData(int? ID)
{
    var model = new StudentModel
    {
         StudentData = stdDef
    };
    return View(model);
}

[HttpPost]
public ActionResult ShowData(Student model) {

【问题讨论】:

标签: asp.net-mvc


【解决方案1】:

这就是它的完成方式。请注意代码中我的cmets:

控制器/模型:

public class StudentModel
{
    public string StudentData { get; set; }
}

public class HomeController : Controller
{
    public string PassDataToMe(StudentModel model)
    {
        return model.StudentData;//no view defined
    }

    public ActionResult ShowData(int? ID)
    {
        var stdDef = "stdDef";
        var model = new StudentModel
        {
            StudentData = stdDef
        };
        return View(model);
    }

    [HttpPost]
    //!changed this to studentmodel
    public ActionResult ShowData(StudentModel model)
    {
        //this is how you pass limited bytes of data that are not encrypted
        return RedirectToAction("PassDataToMe", model);   
    }

查看:

@model Testy20161006.Controllers.StudentModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ShowData</title>
</head>
<body>
    <div> 
        @using (Html.BeginForm())
        {
            @Html.LabelFor(r => r.StudentData);
            @Html.TextBoxFor(r=>r.StudentData);
            <input type="submit" value="submit" />
        }
    </div>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    相关资源
    最近更新 更多