在多个帖子之间保留值

时间:2016-10-04 14:18:26

标签: asp.net asp.net-mvc-4 razor

您好我无法在多个帖子上保留值,并且我的控制器中的“Post1”动作函数始终将MyViewModelObj.Field2作为null。我希望它在第二篇文章中保留旧值 如何使MyViewModel模型类对象保持值?

Mymodels.cs(Model)

namespace RetainTest.Models
{

    public class MyViewModel
    {

        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }
        public string Field4 { get; set; }
    }
}

RetainView.cshtml(查看)

@model RetainTest.Models.MyViewModel

@{
    ViewBag.Title = "RetainView";
}

<h2>RetainView</h2>


@using (Html.BeginForm("Post1", "Retain", FormMethod.Post,
                             new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.Field1);
    @Html.HiddenFor(model => model.Field2);
    @Html.HiddenFor(model => model.Field3);
    @Html.HiddenFor(model => model.Field4);

    <div class="form-horizontal">
        <h4>MyViewModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Field1, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Field1, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Field1, "", new { @class = "text-danger" })
            </div>
        </div>

        @{ 
            if (  Model.Field2 == "Val2")
            { 

                <div class="form-group">
                    @Html.LabelFor(model => model.Field2, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.Field2, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Field2, "", new { @class = "text-danger" })
                    </div>
                </div>

            }
        }
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

RetainController.cs(Controller)

namespace RetainTest.Models
{
    public class RetainController : Controller
    {
        // GET: Retain
        public ActionResult Index()
        {
            MyViewModel MyViewModelObj = new MyViewModel();

            MyViewModelObj.Field1 = "Val1";
            return View("RetainView", MyViewModelObj);
        }
        [HttpPost]
        public ActionResult Post1(MyViewModel MyViewModelObj)
        {
            if (string.IsNullOrEmpty(MyViewModelObj.Field2 ))
            {
                MyViewModelObj.Field2 = "Val2";
            }
            return View("RetainView", MyViewModelObj);
        }

    }
}

2 个答案:

答案 0 :(得分:0)

试试这个:

namespace RetainTest.Models
{

    public class MyViewModel
    {
        [Required(ErrorMessage = "Field1 is required")]
        public string Field1 { get; set; }

        [Required(ErrorMessage = "Field2 is required")]
        public string Field2 { get; set; }

        public string Field3 { get; set; }

        public string Field4 { get; set; }
    }
}

@model RetainTest.Models.MyViewModel

@{
    ViewBag.Title = "RetainView";
}

<h2>RetainView</h2> 
@using (Html.BeginForm("Post1", "Retain", FormMethod.Post, new { id = "form", enctype = "multipart/form-data"}))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.Field1);
@Html.HiddenFor(model => model.Field2);
@Html.HiddenFor(model => model.Field3);
@Html.HiddenFor(model => model.Field4);

<div class="form-horizontal">
    <h4>MyViewModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        <Label for="Field1" class="control-label col-md-2">Field1 </label>
        <div class="col-md-10">
            @if(Model.Field1 == "Val1")
            {
                <input type="text" name="Field1" id="Field1" class="form-control" value="@Model.Field1">
            }
            else 
            {
                <input type="text" name="Field1" id="Field1" class="form-control">
            }
            <span class="field-validation-valid text-danger" 
                    data-valmsg-for="Field1" 
                    data-valmsg-replace="true">
            </span>
        </div>
    </div> 
    <div class="form-group">
        <Label for="Field2" class="control-label col-md-2">Field2 </label>
        <div class="col-md-10">
            @if(Model.Field2 == "Val2")
            {
                <input type="text" name="Field2" id="Field2" class="form-control" value="@Model.Field2">
            }
            else 
            {
                <input type="text" name="Field2" id="Field2" class="form-control">
            }
            <span class="field-validation-valid text-danger" 
                data-valmsg-for="Field2" 
                data-valmsg-replace="true">
            </span>
        </div>
    </div>              
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
     @Scripts.Render("~/bundles/jqueryval")
}


namespace RetainTest.Models
{
    public class RetainController : Controller
    {
        private MyViewModel MyViewModelObj = new MyViewModel();

        // GET
        public ActionResult Index()
        {       
            if (Session["MyObj"] != null)               
                MyViewModelObj = (MyViewModel)Session["MyObj"];             
            else 
                MyViewModelObj.Field1 = "Val1";             

            return View("RetainView", this);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Post1(FormCollection form)
        {
            MyViewModelObj.Field1 = form.Get("Field1");
            MyViewModelObj.Field2 = form.Get("Field2");

            if (string.IsNullOrEmpty(MyViewModelObj.Field2))                
                MyViewModelObj.Field2 = "Val2";             

            // here you need to store the data somewhere! 
            // session, database.
            // just an example: 
            Session.Add("MyObj", MyViewModelObj);

            return View("RetainView", this);
        }
    }
}

我使用会话来保留对象的值,但还有一些其他方法来存储数据。上面的代码会将用户输入发布到控制器并在会话中保留其值。

答案 1 :(得分:0)

Stephen Muecke的回答

&#34;您的视图为模型的每个属性都有一个隐藏的输入。 DefaultModelBinder读取请求中每个属性的第一个名称/值对,并忽略所有其他属性。由于隐藏的输入是第一个,因此将忽略EditorFor()方法的值。您只能绑定模型的初始值,而不是编辑的值(使您的表单毫无意义)。删除隐藏的输入!&#34;

相关问题