asp .net mvc 4.0, vs10 , razorview
This is my listbox :
<div class="editor-list-field">
<select name="lbToCreateTable" class="clsToCreateTable" id="idToCreateTable" style="width:150px" size="8" multiple="multiple" >
@if(Model.ColumnsToCreateTable != null)
{
<option>@Model.ColumnsToCreateTable;</option>
}
</select>
</div>
my Controller :
public ActionResult About(AboutModel model, string btnTestMail, string btnSendMail, string lbToCreateTable)
{
try
{
if (btnTestMail != null)
{
List<string> lstTableCreateCols = new List<string>();
try
{
if (Request.Form["lbToCreateTable"].ToString().Length > 0)
{ }
HtmlString hs = new HtmlString(model.Content);
if (model.TableCreate)
{
if (hs.ToString().Length > 0 && model.CompanyName != "" && model.MailSubject != "")
{
CM.CreateMailBody(model.CompanyName, model.MailSubject, hs);
}
}
else
{
if (hs.ToString().Length > 0 && model.CompanyName != "" && model.MailSubject != "" && model.ColumnsToCreateTable != null)
{
List<string> lstColumnsToCreateTable = new List<string>();
foreach (SelectListItem str in model.ColumnsToCreateTable)
{
lstColumnsToCreateTable.Add(str.Selected.ToString());
}
CM.CreateMailbodyWithTable(model.CompanyName, model.MailSubject, hs, lstColumnsToCreateTable);
}
}
}
catch { }
}
else if (btnSendMail != null)
{
}
}
catch { }
try
{
if (btnTestMail == null && btnSendMail == null)
{
if (Session["CompanyName"].ToString().Length > 0)
{
RedirectFromHomeToAbout(model);
}
}
}
catch { }
return View(model);
}
my model:
[Required]
[DataType(DataType.Text)]
public string CompanyName { get; set; }
[Required]
[DataType(DataType.Text)]
public string MailSubject { get; set; }
public List<string> TableColumn { get; set; }
public bool IsChecked { get; set; }
public List<string> ColumnsToCreateTable { get; set; }
public bool TableCreate { get; set; }
[Display]
[DataType(DataType.Text)]
public string Message { get; set; }
[Required]
[DataType(DataType.Html)]
[AllowHtml]
public string Content { get; set; }
[DataType(DataType.EmailAddress)]
public string EmailAddress { get; set; }
in my model why i am getting Model.ColumnsToCreateTable = null;
Bussiness :
when my view loads, this Listbox will be blank. In this view, there is another Listbox, which is populated correctly. I will populate the ListBoxToCreateTable(which was blank at first). when i submit the form, along with all other data, this list(of newly populated listbox) will sent to my contrller. And i have to do a lot of things with this.
Question :
- How could i solve this issue?
- is this the right way to do this?
- any other suggestion?
model.ColumnsToCreateTable
is not null before adding items to itmodel.ColumnsToCreateTable.Add(str)
. If so then add this before adding itemsmodel.ColumnsToCreateTable = new List<string>();
– Hary Jan 29 '13 at 9:46model
toview
. post the fullcontroller
code – Hary Jan 29 '13 at 10:12