0

在我的 ASP.NET MVC 应用程序中,我只想更新 Register 方法中的两个变量。

我的控制器中的注册方法如下所示(它没有自己的视图,所以我不确定在这种情况下要返回什么):

[HttpPost]
public ActionResult Register(Container c)
{

String email = c.register.Email.ToString();
String password = c.register.Password.ToString();
return view();
}

我的索引视图如下所示;

@model VoterMvcTest.Models.Container    

@using(Html.BeginForm("Register", "Default1")) 
{
@Html.LabelFor(o => o.register.Email)
@Html.EditorFor(o => o.register.Email)
@Html.LabelFor(o => o.register.Password)
@Html.EditorFor(o => o.register.Password)
<input class="button" type="submit" value="Register" />
}

我的模型看起来像这样;

public class Container
{
    public Login login { get; set; }
    public Register register { get; set; }

}

public class Login
{
    public string Email { get; set; }
    public string Password { get; set; }
}

public class Register
{
    public string Email { get; set; }
    public string Password { get; set; }

}

当您单击按钮时,我希望设置电子邮件和密码这两个变量,并且可以在另一种方法中使用。我不确定我应该返回什么(目标是保持相同的观点)。我试图重定向到我当前所在的同一视图(索引视图),但似乎这会导致回发并且变量丢失。

4

2 回答 2

0
ASP.NET MVC does not have ViewState as you would have when using WebForms. So if you 
perform a post and do not persist the data in either a database or a temporary memory 
(session object for instance) you'll lose that data as soon as the request has finished.
If you do some kind of validation and determine that the user was well registered you
could redirect him to another view using: 

    return RedirectToAction("Index")
于 2013-03-19T19:32:53.883 回答
0

试试这个

return View("Index",c)

它会起作用的

于 2013-07-15T19:00:11.847 回答