How can I transfer information of one kind to another?

I developed a view in asp.net mvc3 outside the course registration form. This is a very simple form with the name, father’s name, qualification and submit button, after clicking the submit button I want to display information using a different view. please suggest me how can I send information from one view to another.

my controller class:

namespace RegistrationForm.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
           // ViewBag.Message = "Welcome to ASP.NET MVC!";

            //return View();
          return  RedirectToAction("registrationView");
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult registrationView()
        {
            return View();
        }
    }
}

My opinion:

@ {Layout = null; }

  registrationView Enter a name                               

       </td>
       <tr>
       <td>
          Enter Father Name
       </td>
       <td>
        <input type="text" name="fname" id="fname" />


       </td>
       <tr>
       <td>
          Enter Qualification
       </td>
       <td>
        <input type="text" name="qly" id="qly" />


       </td>

  </tr>
</table>
    <input type="submit" value="submit" />
</div>

+3
source share
5 answers

, , - , , .

, ""

ex: RegistrationModel.cs .

2 :

1- GET, RegistrationModel , :

return View();

- :

var registrationModel = new registrationModel();
return View(registrationModel);

2- POST Action, -

[HttpPost]
public ActionResult registrationView(RegistrationModel model)    
{
  // your code goes here
}

, . View "" , MVC . .

-. ,

+2

ViewData e. , , , , .

namespace RegistrationForm.Controllers { public class HomeController : Controller { public ActionResult Index() { // ViewBag.Message = "Welcome to ASP.NET MVC!";
    ViewData["myData"] = "hello world";

        //return View();
      return  RedirectToAction("registrationView");
    }

    public ActionResult About()
    {
        return View();
    }

    public ActionResult registrationView()
    {
        // get back my data
        string data = ViewData["myData"] != null ? ViewData["myData"].ToString() : ""; 

        return View();
    }
}

ViewData html/aspx/ascx registerView.

, registerView.aspx:

<div id="myDiv">
my data was: <%= ViewData["myData"] %>
</div>
0
<% using Html.Form("<ActionName>") { %>
    // utilize this HtmlHelper action to redirect this form to a different Action other than controller that called it.
<% } %>
0

You can simply specify parameters with the name of the controls in the declare method parameter list. For example:

The control here has id "qly" <input type="text" name="qly" id="qly" />

Define the list of parameters of your method as follows: public ActionResult YourMethod(string qly) { //simply pass your qly to another view using ViewData, TempData, or ViewBagand use it as needed
}

0
source

You must useTempData one that was made specifically for him in order to preserve values ​​between actions.

This example is from MSDN (link above):

public ActionResult InsertCustomer(string firstName, string lastName)
{
    // Check for input errors.
    if (String.IsNullOrEmpty(firstName) ||
            String.IsNullOrEmpty(lastName))
    {
        InsertError error = new InsertError();
        error.ErrorMessage = "Both names are required.";
        error.OriginalFirstName = firstName;
        error.OriginalLastName = lastName;
        TempData["error"] = error; // sending data to the other action
        return RedirectToAction("NewCustomer");
    }
    // No errors
    // ...
    return View();
}

And to send data to the view, you can use the or model ViewBag.

0
source

All Articles