I'm trying to get the new textarea value to read into my controller so that way I can store the new value in my SQl database.
View:
@{
ViewBag.Title = "TRIO FAQ Edit";
}
<h2>@ViewBag.Title (@Html.ActionLink("Done", "FAQ", "Home"))</h2>
<a class="btn btn-primary" href="@Url.Action("FAQAdd","Home")">Create New</a>
<!-- Bootstrap FAQ - START -->
<div class="container">
<br />
@foreach (var item in Model)
{
var q = $"questfor{@item.QuestionID1}";
var a = $"ansfor{@item.QuestionID1}";
<form id='@string.Format("formfor{0}", @item.QuestionID1)' runat="server">
<h4>Question:</h4>
<textarea runat="server" name='@string.Format(q)' id='@string.Format(q)' style="max-height: 100px; height:100px; width: 100%;">@item.QuestionValue</textarea>
<h4>Answer:</h4>
<textarea runat="server" name='@string.Format(a)' id='@string.Format(a)' style="max-height: 100px; height:100px; width: 100%;">@item.AnswerValue</textarea>
<br />
<a class="btn btn-primary" href="@Url.Action("FAQDelete","Home", new { value=@item.QuestionID1})">Delete</a>
<a class="btn btn-primary" href="@Url.Action("FAQSaveEdit","Home", new { value=@item.QuestionID1, question=q.Text})">Save</a>
<br />
</form>
}
</div>`
`
Controller:
public ActionResult FAQSaveEdit(int value)
{
//my sqlconnection is fine, i dont want to share the value
//SqlConnection c = new SqlConnection;
c.Open();
string gr = Request.Form[$"questfor{value}"].ToString();
string gre = Request.Form[$"ansfor{value}"].ToString();
SqlCommand cmd = new SqlCommand($"UPDATE FAQ SET Question={gr}, Answer={gre} WHERE QuestionID={value}", c);
cmd.ExecuteNonQuery();
SqlCommand cmd2 = new SqlCommand("SELECT * FROM FAQ", c);
var model = new List<Models.FAQEntry>();
SqlDataReader rdr = cmd2.ExecuteReader();
while (rdr.Read())
{
var faqent = new Models.FAQEntry();
faqent.QuestionID1 = (int)rdr["QuestionID"];
faqent.QuestionValue = (string)rdr["Question"];
faqent.AnswerValue = (string)rdr["Answer"];
model.Add(faqent);
}
c.Close();
return View(model);
}
Model:
namespace fproj.Models
{
public class FAQEntry
{
public int QuestionID1 { get; set; }
public string QuestionIDWord { get; set; }
public string QuestionValue { get; set; }
public string AnswerValue { get; set; }
}
}
I've had no issues with adding and deleting entries, with the adding being a generic response. However, I'm not sure how to read the value when changed in a textarea. I've tried switching to input instead, but i keep getting warnings for one reason or another. .Text tells me it won't work with a string and .Form may return null when its not supposed to. Any advice would be helpful, as I've been at this for awhile. Thank you!