Reputation: 1288
I am freshman in MVC, so I have some problems. I want to create Create Form, where I can insert some information such as Country, Region etc. and after to Serialize it to the file. But my Create Method get object wich don't have any indormation. So where I did mistake?
[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);
}
This is my VIEW FILE
@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
}
}
Upvotes: 1
Views: 103
Reputation: 1979
The View and the controller action code all look fine to me. The issue will be in the address class in the model. Mvc needs to have properties to deserialise the webforms post to.
public string Country { get; set; }
You should name these using title case for convention but it would still work if you kept them lowercase.
Upvotes: 1