0

I am trying to pass data of method to another method. Can you help me please?

Here is my code: return View(model); model should be pass public ActionResult ShowData(Student model) method.

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

[HttpPost]
public ActionResult ShowData(Student model) {
harry
  • 25
  • 4
  • This link may be helpful for you: http://stackoverflow.com/questions/4816869/mvc-pass-parameter-to-view – Tunahan Apr 18 '17 at 07:56
  • Your code snippet seems to be incomplete. Can you add the missing rest? – Marco Apr 18 '17 at 07:56
  • Possible duplicate of [Can we pass model as a parameter in RedirectToAction?](http://stackoverflow.com/questions/22505674/can-we-pass-model-as-a-parameter-in-redirecttoaction) – Hina Khuman Apr 18 '17 at 08:05
  • @Hina Khuman My question isnot pass in redirect to action.I want to pass defined model to HttpPOST public ActionResult ShowData(Student model – harry Apr 18 '17 at 08:12
  • @harry: It seems unclear to me! What you want to exactly? why do you want that? – Hina Khuman Apr 18 '17 at 08:17
  • @harry is the answer I provided adequate? – kblau Apr 19 '17 at 18:33

1 Answers1

0

This is how it is done. Please pay attention to my comments in the code:

Controller/Model:

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);   
    }

View:

@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>
kblau
  • 2,094
  • 1
  • 8
  • 20