Solution for selecting and editing data at the same time

I have an asp.net application in C #.

I have a table in a SQL Server database with two fields:

Field1 Comments 

I need to allow the user SELECT Field1 and add comments for this particular Field1

the user will then post the login back to the server

how to do it? what control can i use? is there any jquery simple solution already?

+1
source share
2 answers

It completely depends on the settings of your site. However, if you have a list of Field1 values ​​and put them in a DropDownList, you can select it and add a comment to the field.

It also depends heavily on your user interface / workflows. This is just one of MANY possible ways to do this.

Code front

 <asp:DropDownList ID="ddlField1" runat="server"> <asp:ListItem>Value1</asp:ListItem> <asp:ListItem>Value2</asp:ListItem> <asp:ListItem>Value3</asp:ListItem> <asp:ListItem>Value4</asp:ListItem> </asp:DropDownList> <asp:TextBox ID="tbComments" runat="server" TextMode="MultiLine"></asp:TextBox> <asp:Button ID="btSubmit" runat="server" OnClick="btSubmit_Click"></asp:Button> <asp:SqlDataSource ID="sqlRecord" runat="server" InsertCommand="InsertRecord" InsertCommandType="StoredProcedure"> <InsertParameters> <asp:Parameter Name="Field1" Type="String" /> <asp:Parameter Name="Comment" Type="String" /> </InsertParameters> </asp:SqlDataSource> 

Code for

 public void btSubmit_Click(object sender, EventArgs e) { // Do your validation of the data here .. // Add fields and insert sqlRecord.InsertParameters["Field1"].DefaultValue = ddlField1.SelectedValue; sqlRecord.InsertParameters["Comment"].DefaultValue = tbComment.Text; sqlRecord.Insert(); } 

This is unverified and incomplete pseudocode, but should lead you in the right direction for a non-MVC general web application. Nor does it use JavaScript / jQuery, just ASP.NET and C #.

+1
source

I would put the possible options in the drop-down list, and then use jquery to make a JSON request to get data from your controller, and then a save button that calls another javascript function to send the changed data back to the controller.

controller:

 public ActionResult GetFields() { var model=new MyModel(); model.Selections =(from m in database.table select m.Field1).ToList(); return View(model); } public ActionResult GetComments(string field1) { return Content((from c in database.table where(c.Field1==field1)select c.Comments).First()); } [HttpPost] public void SaveComments(string field1, string comments) { var record=(from r in database.table where(r.Field1==field1)select r).First(); record.Comments=comments; database.SaveChanges; return; } 

View:

 <script type="text/javascript> function SaveComments(){ var url='@Url.Action("GetComments","YourControllerName"); url+='?field1='+$('#selections option:selected').text(); $('#selectedField').text($('#selections option:selected').text()); $('#commentEditor').load(url); }; function SaveComments(){ var url='@Url.Action("SaveComments","YourControllerName")'; url+='?field1='+$('#selections option:selected').text()+'&comments='+$('#commentEditor').text(); $('#dummy').load(url); }; </script> <select id="selections" onchange="SelectionChanged()"> @foreach(var item in Model.Selections) { <option value="@item">@item</option> } </select> <div id="selectedField"/> <input type="text" id="commentEditor"/> <input type="button" value="apply" onclick="SaveComments()"/> <div id="dummy"/> 

Disclaimer: there may be small errors (usually do not write large chunks of code in the text box: P), you probably should not use a dummy div, and you will need to have a List in your view model whose return returns to based on values ​​in Selections. This should give you a general idea.

+2
source

All Articles