cazlouise cazlouise - 2 days ago 5
ASP.NET (C#) Question

How to Update a database column after changes from front end ASP.NET Mvc? Create a CheckBox and make it READONLY?

I have created an application for several stores to fill in a questionnaire, the application has several tables within the database. Two of them are the ones I am focusing on which are Audit and Storequestions. The Audit table contains the data for the stores for example: storename, and its primary key is “AuditId”. The storesquestion table contains all the questionnaire table and AuditId as a foreign key form the Audit table.
I am wanting to create a Read Only tick box as an indication in order to find out which stores has completed the questionnaire as the only way I know which has completed the questionnaire is by looking in the database. My approach to re4solve this matter was to create a new column called read only within the Audit table with a Boolean datatype so when a store has completed the questionnaire it will set the read only row to 1(true).
Could do with a bit of Help as I don’t seem to go forward at the moment

Thanks in advance

public ActionResult Create()
{
StoreQuestions sq = new StoreQuestions();
sq.AuditId = (int)System.Web.HttpContext.Current.Session["AuditId"];


return View(sq);
}

//
// POST: /StoreQuestions/Create

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(StoreQuestions storequestions)
{

if (ModelState.IsValid)
{
db.StoreQuestions.Add(storequestions);
db.SaveChanges();
return RedirectToAction("Details", "Audit", new { id = storequestions.AuditId });
}

return View(storequestions);
}


ViewModel

public class MainModel
{
public StoreAudit StoreAudit { get; set; }
public StoreQuestions StoreQuestion { get; set; }
public List<StoreAudit> StoreAuditList { get; set; }
public List<StoreQuestions> StoreQuestionsList { get; set; }
public List<User> User { get; set; }
public List<string> StoreWindow { get; set; }
}


}

Answer

Okay I think I understand what you're trying to do. In order to update the readonly property to true you have to find the audit record and then change the value of that record's readonly property.

Like so:

[HttpPost]
[ValidateAntiForgeryToken]
    public ActionResult Create(StoreQuestions storequestions)  
    {

        if (ModelState.IsValid)
        {
            Audit findingAudit = db.AuditTable.Find(storequestions.AuditId);
            // db being your connectionstring property
            findingAudit.readonly = true;
            db.StoreQuestions.Add(storequestions);
            db.SaveChanges();
            return RedirectToAction("Details", "Audit", new { id = storequestions.AuditId });
        }

        return View(storequestions);
    }

I hope this helps!

Comments