I'm trying to do a data entry to be collected in a collection list via partial view and refresh the list before final saving. The result of my trial was it didn't refresh the list and it shows the partial section not with the entry section plus the listing.
I tried this one :
Model
<pre lang="ASP.NET">
public class Tweet
{
public Tweet()
{
Date = System.DateTime.UtcNow;
}
private DateTime date;
public int Id { get; set; }
public string UserName { get; set; }
public string Text { get; set; }
public DateTime Date
{
get { return date.ToLocalTime(); }
set { date = value; }
}
}
public class TwitterViewModel
{
public TwitterViewModel() {
Tweet = new Tweet();
Tweets = new List<Tweet>();
}
public Tweet Tweet { get; set; }
public IList<Tweet> Tweets { get; set; }
}
</pre>
Tweet View :
<pre lang="ASP.NET">@model Test.Models.TwitterViewModel
@{
ViewBag.Title = "Twitter";
}
<hgroup class="title">
<h2>Tweet Test</h2>
</hgroup>
<div style="width: 600px; border: 1px solid silver">
@using (Ajax.BeginForm("AddTweet", "TestReference", new AjaxOptions { UpdateTargetId = "AllTweets" }))
{
<div style="width: 600px; border-width: 1px">
@Html.TextAreaFor(md => md.Tweet.Text)
<input type="submit" value="Add" style="width: 70px; font-size: 12px" />
</div>
<div id="AllTweets">
@Html.Partial("_AllTweets", Model)
</div>
}
</div></pre>
_AllTweet Partial View :
<pre lang="ASP.NET">
@model Test.Models.TwitterViewModel
@using (Html.BeginForm("SaveTweet", "TestReference"))
{
<div style="height: 300px; width: 600px; overflow-y: auto">
<table border="1">
@if (Model != null && Model.Tweets != null)
{
for (int i = 0; i < Model.Tweets.Count; i++)
{
<tr style="vertical-align: top">
<td style="font-size: 12px; padding-left: 2px">
@Html.DisplayFor(modelItem => Model.Tweets[i].Date) -
@Html.DisplayFor(modelItem => Model.Tweets[i].UserName) :
</td>
<td>
@Html.Raw(Model.Tweets[i].Text.Replace("\r\n", "<br />"))
</td>
</tr>
@Html.HiddenFor(modelItem => Model.Tweets[i].Id)
@Html.HiddenFor(modelItem => Model.Tweets[i].Date)
@Html.HiddenFor(modelItem => Model.Tweets[i].Text)
@Html.HiddenFor(modelItem => Model.Tweets[i].UserName)
}
}
</table>
</div>
<input type="submit" value="Save" style="float: right; width: 70px; font-size: 12px" />
}
</pre>
Controller :
<pre lang="ASP.NET">
public ActionResult ShowTweet()
{
TwitterViewModel model = new TwitterViewModel();
return View("Tweets", model);
}
[HttpPost]
public ActionResult AddTweet(TwitterViewModel viewModel)
{
try
{
viewModel.Tweets.Add(viewModel.Tweet);
return PartialView("_AllTweets", viewModel);
}
catch
{
return View();
}
}
[HttpPost]
public ActionResult SaveTweet(TwitterViewModel viewModel)
{
try {
// this section for saving
RedirectToAction("ManageTweets");
}
catch(Exception except){
return View();
}
}
</pre>
}
What am i missing in my codes?