Asp.net MVC在不使用html助手的情况下重新填充表单

时间:2015-07-29 20:13:43

标签: c# asp.net asp.net-mvc

如果我在没有填写某些必填字段的情况下提交表单,当页面重新加载时会出现错误,填写的字段会保留其值,但前提是我使用html帮助程序,如下所示:

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    <p>Your name: @Html.TextBoxFor(x => x.Name) </p>
    <p>Your email: @Html.TextBoxFor(x => x.Email)</p>
    <p>Your phone: @Html.TextBoxFor(x => x.Phone)</p>
    <p>
        @Html.DropDownListFor(x => x.WillAttend, new[] {

        <input          new SelectListItem() {
                            Text = "Yes, I'll be there",
                            Value = bool.TrueString},
                        new SelectListItem() {
                            Text = "No, I can't come",
                            Value = bool.FalseString}
                        }, "Choose an option")
    </p> type="submit" value="Submit RSVP" />
}

但我想使用常规的html而不是这样的帮助(然后记住这些值):

<form action="/Home/RsvpForm" method="post">
    @Html.ValidationSummary()
    <p>
        Your name:
        <input type="text" name="Name" value="" />
    </p>
    <p>
        Your email:
        <input type="text" name="Email" value="" />
    </p>
    <p>
        Your phone:
        <input type="text" name="Phone" value="" />
    </p>
    <p>
        Will you attend?
        <select name="WillAttend">
            <option value="">Choose an option</option>
            <option value="true">Yes</option>
            <option value="false">No</option>
        </select>
    </p>
    <button>Submit</button>
</form>

那么如何在不使用html助手的情况下让表格记住正确输入的值,是否可能?

编辑:模型和整个视图

GuestResponse模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace PartyInvites.Models
{
    public class GuestResponse
    {
        [Required(ErrorMessage="Enter name")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Enter email")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Enter phone")]
        public string Phone { get; set; }

        [Required(ErrorMessage = "Specify")]
        public bool? WillAttend { get; set; }



    }
}

整个观点:

@model PartyInvites.Models.GuestResponse

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>RsvpForm</title>
</head>
<body>
    <form action="/Home/RsvpForm" method="post">
        @Html.ValidationSummary()
        <p>
            Your name:
            <input type="text" name="Name" value="" />
        </p>
        <p>
            Your email:
            <input type="text" name="Email" value="" />
        </p>
        <p>
            Your phone:
            <input type="text" name="Phone" value="" />
        </p>
        <p>
            Will you attend?
            <select name="WillAttend">
                <option value="">Choose an option</option>
                <option value="true">Yes</option>
                <option value="false">No</option>
            </select>
        </p>
        <button>Submit</button>
    </form>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

您可以使用常规html,只需设置输入文本的值,即可接收您的model.property,如:

<input type="text" name="Phone" value="@Model.Phone" />

答案 1 :(得分:2)

类似的东西:

@model your_model 

 <form action="/Home/RsvpForm" method="post">
@Html.ValidationSummary()
<p>
    Your name:
    <input type="text" name="Name" value="@Model.Name" />
</p>
<p>
    Your email:
    <input type="text" name="Email" value="@Model.Email" />
</p>
<p>
    Your phone:
    <input type="text" name="Phone" value="@Model.Phone" />
</p>
<p>
    Will you attend?
    <select name="WillAttend">
        <option value="">Choose an option</option>
        <option value="true">Yes</option>
        <option value="false">No</option>
    </select>
</p>
<button>Submit</button>

答案 2 :(得分:1)

你的模型是null。 Null没有电话号码的概念,因为没有对象。这就是为什么你在尝试无中断地访问Phone时遇到错误的原因。每次尝试访问Model的属性时,都需要处理null case。

您可以将此用于您的财产:

@(Model != null ? Model.Phone : "") 

或者,如果您能够使用C#6语法,请随意使用:

@Model?.Phone