与MVC的麻烦

时间:2014-02-25 21:51:37

标签: c# asp.net-mvc

我是MVC的新生,所以我遇到了一些问题。我想创建创建表单,我可以在其中插入一些信息,如国家,地区等,然后将其序列化到文件。但我的创建方法得到的对象没有任何错误。所以我错了?

 [HttpPost] // This is my Method which must Serialize class Address
    public ActionResult Create(Address address)
    {
        if (ModelState.IsValid)
        {
            // Размещаем в XML данные
            System.Xml.Serialization.XmlSerializer writer =
                new System.Xml.Serialization.XmlSerializer(typeof(Address));

            System.IO.StreamWriter file = new System.IO.StreamWriter(@"c:\MvcApp.xml");
            writer.Serialize(file, address);
            file.Close();
            return RedirectToAction("Index");
        }
        return View(address);
    }

这是我的查看文件

 @model WorkWithAddresses.Models.Address

 @{
ViewBag.Title = "Create";
 }

 <h2>Create</h2>

 @using (Html.BeginForm())
 {
@Html.ValidationSummary(true)
    <fieldset>
        <legend>Address</legend>
        <div class="editor-label">
            @Html.LabelFor(model=>model.country, "Country")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model=>model.country)
            @Html.ValidationMessageFor(model=>model.country)
        </div>

        <div class ="editor-label">
            @Html.LabelFor(model=>model.region, "Region")
        <div>
        <div class="editor-field">
            @Html.EditorFor(model=>model.region)
            @Html.ValidationMessageFor(model=>model.region)
        </div>

            <div class ="editor-label">
                @Html.LabelFor(model=>model.locality,"Locality")
            </div>
            <div class ="editor-field">
                    @Html.EditorFor(model=>model.locality)
                    @Html.ValidationMessageFor(model=>model.locality)
            </div>

             <div class ="editor-label">
                @Html.LabelFor(model=>model.locality,"Street")
            </div>
            <div class ="editor-field">
                    @Html.EditorFor(model=>model.street)
                    @Html.ValidationMessageFor(model=>model.street)
            </div>

            <div class ="editor-label">
                @Html.LabelFor(model=>model.locality,"House")
            </div>
            <div class ="editor-field">
                    @Html.EditorFor(model=>model.houseNumber)
                    @Html.ValidationMessageFor(model=>model.houseNumber)
            </div>

            <div class ="editor-label">
                @Html.LabelFor(model=>model.locality,"Building")
            </div>
            <div class ="editor-field">
                    @Html.EditorFor(model=>model.buildingNumber)
                    @Html.ValidationMessageFor(model=>model.buildingNumber)
            </div>

             <div class ="editor-label">
                @Html.LabelFor(model=>model.locality,"Apartment")
            </div>
            <div class ="editor-field">
                    @Html.EditorFor(model=>model.apartmentNumber)
                    @Html.ValidationMessageFor(model=>model.apartmentNumber)
            </div>

            <p>
                <input type="submit" value="Create"/>
            </p>
            </fieldset>
}

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

 // This is my class which I want to Serialize after user put information
[XmlRoot("Address")]
public class Address
{
    #region Constructors
    public Address()
    {
    }
    #endregion

    #region Public Member Variables
    [XmlElement("AddressId")]
    public int addressId;              // Id адреса
    [XmlElement("Country")]
    public string country;            // Страна
    [XmlElement("Region")]
    public string region;             // Регион
    [XmlElement("Locality")]
    public string locality;           // Населенный пункт
    [XmlElement("Street")]
    public string street;             // Улица
    [XmlElement("HouseNumber")]
    public string houseNumber;        // Номер дома
    [XmlElement("BuildingNumber")]
    public string buildingNumber;     // Номер корпуса
    [XmlElement("ApartmentNumber")]
    public string apartmentNumber;    // Номер квартиры
    [XmlElement("Description")]
    public Description description;   // Описание адреса
    #endregion
  }
 }

1 个答案:

答案 0 :(得分:1)

View和控制器动作代码对我来说都很好看。问题将出在模型中的地址类中。 Mvc需要具有将webforms帖子反序列化的属性。

public string Country { get; set; }

您应该使用标题大小写为惯例,但如果将它们保持小写,它仍然可以使用。