简体   繁体   English

在MVC 3中保持状态

[英]Maintaining state in MVC 3

I am simply devloping entry form using mvc 3. I have many text boxes,radio button, lables. 我只是使用MVC 3开发报名表。我有很多文本框,单选按钮,标签。 so while post back data from view to controller it loses entered data. 因此,在将数据从视图回发到控制器时,它会丢失输入的数据。 how to retain this for all postbacks? 如何保留所有回发? is there any mvc mechanisam avialable by default? 默认情况下有没有可用的mvc mechanisam? which means by enabling some properties do we command mvc to handle state by itself? 这意味着通过启用某些属性,我们可以命令mvc自行处理状态?

i am not interested to send viewdata object to view. 我不希望发送viewdata对象进行查看。 except that what is default mechanism availble in mvc3? 除了mvc3中可用的默认机制是什么?

Update: 更新:

@using (Html.BeginForm())
{
<table>
    <tr>
        <td>

            @Html.TextBox("txtTitle")
@Html.CheckBox("chkOption")
        </td>
        <td>               
            @Html.TextBox("txtDetails")
@Html.DropDownList("drpList")
        </td>
        <td>
            <input type="submit" value="Cancel" name="action" />
            <input type="submit" value="Create" name="action" />

        </td>
    </tr>
</table>
}

How to maintain state for entered and selected value for all post backs? 如何维护所有回发的输入值和选定值的状态?

您可以使用会话在多个回发之间保持状态。

It looks like you need to create a Model and use the @Html.TextBoxFor() , @Html.CheckBoxFor() and @Html.DropdownListFor() methods instead, on the properties of your model. 看来您需要创建一个Model然后在模型的属性上使用@Html.TextBoxFor()@Html.CheckBoxFor()@Html.DropdownListFor()方法。

You would then have a similarly named action method, decorated with a [HttpPost] attribute and the model as a parameter. 然后,您将具有一个类似命名的操作方法,该方法以[HttpPost]属性和模型作为参数进行修饰。 You will then be able to perform validation on attempted form submissions and process the model if the form state is valid. 然后,您将能够对尝试提交的表单执行验证,并在表单状态有效的情况下处理模型。

I'd look into researching strongly typed views, models and maybe go through a few online tutorials. 我将研究强类型视图,模型,并可能会通过一些在线教程进行研究。

The link included below seems a good tutorial, it's a few pages long, but should go through everything you need to get started. 下面包含的链接似乎是一个很好的教程,虽然只有几页,但应该涵盖了入门所需的所有内容。

http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3 http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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