简体   繁体   中英

How to re-use data from a previous post to be used in a new one with ASP .NET MVC 4, C# and Razor?

Greeting, everybody.

I am new to ASP .NET MVC and Razor, and I am stuck with a problem: I need the data entered to a form to be available in the same form, after being saved to database, if a certain checkbox is checked. I have found plenty of information about Postback and Crosspage, but none of the articles/tutorials/references details the way to use them. For example, where exactly, in Visual Studio 2012 Express can I see the Page_Load() event?

Thanks in advance.

MVC4 is based on the Model View Controller design so the PageLoad() Method isn't really applicable.

For Example what you may be looking for is this

Model

public class SampleModel
{
    public int ModelId {get; set;}        
    public string ModelName {get; set;}        
}

Controllers

[HttpGet]
public ActionResult SampleController()
{
    return View();
}

[HttpPost]
public ActionResult SampleController(SampleModel model)
{
    //put code here to send to database
    return View(model);
}

View

@model YourProject.Models.SampleModel

@using (Html.BeginForm())
{
    @Html.LabelFor(model => model.ModelId)
    @Html.TextBoxFor(model => model.ModelId)
    <br />
    @Html.LabelFor(model => model.ModelName)
    @Html.TextBoxFor(model => model.ModelName)

   <input type="submit" value="submit" />
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM