[RESOLVED]write calculated value to table (asp.net mvc)

I am new to MVC …

I created a simple application to calculate the score. There are 2 sets of radio buttons. User will select selection option and it will calculate the score from the selections. I am able to do all of that but I want to write the calculated score to a field
of the record. How can I do that?

Simplified table as below:

ID   Name       RadioSelected1    RadioSelected2              Score

1    Project1    3                           1                                       ????

Simplified formular:   Score = (3+1)/2 = 2, now I need to write value 2 to where Score is the  record ID 1.

Thanks

 

Hi,

Can u clarify, do we want to update Score to DB (at serverside)? or just want to show in another field (like textbox) at clientside ?

If it is to updated the db record at server

using (SqlConnection connection = new SqlConnection(MyConString))
            {
                try
                {
                    string cmdText = "Update Project SET Score=@Score WHERE ID=@ID";
                    SqlCommand cmd = new SqlCommand(cmdText, connection);
                    cmd.Parameters.AddWithValue("@Score", calculatedscore);
                    cmd.Parameters.AddWithValue("@ID", recordID);
                    connection.Open();
                    int result = cmd.ExecuteNonQuery();
                   
                }
                catch (Exception ex)
                {

                }
            }

If it is to update the client html table score column, it depends how you are calculate. suppose if you u r calculating by click on the button on same row

$('.buttonclassname').each(function(){
	$(this).click(function(){
		//do the calc
		$(this).siblings('#lblscore').text(calculatedone);
	});
});

I need to write it to the DB, at the server side.

Thanks in advance

As mentioned, I am new and still trying to get the concept of this MVC….

I have the code to calculate the score in edit view just below the radio button sets.

if
(Model.project.LegalID == 1)

{    legalPoint = 3;}

else
if(Model.project.LegalID
== 2)

{ legalPoint = 2; }

else 

.

.

Hope this detail helps

Can you elaborate a bit more… where does your code go? the view??

Hi,

dccmvc

I have the code to calculate the score in edit view just below the radio button sets.

Do u have Razor code to calculate score in Edit View?

Then that doesn’t work, since ur code works only at serverside.

For clientside, u have to use js/jquery and save it in hidden field, so that during postback it can be accessed at serverside actionMethod.

Yes I have Razor code. Can you or someone take step further and provide some code (which will help me a ton)?

I did try this code as below in Edit View but did not write the value to the destination field "legalPoint":

————Radio Buttons—————————————————————————————–

@Html.RadioButtonFor(model => model.project.LegalID,"1",
new
{ onclick =
"LegalInfoChanged(’1′)"
})
<br
/>

@Html.RadioButtonFor(model => model.project.LegalID,"2",
new
{ onclick =
"LegalInfoChanged(’2′)"
})
<br
/>

@Html.RadioButtonFor(model => model.project.LegalID,"2",
new
{ onclick =
"LegalInfoChanged(’2′)"
})
<br
/>

———On
click will call LegalInfoChanged function as below:——————–

<script
type="text/javascript">

function
LegalInfoChanged(mySelection) {

if (mySelection ==
’1′)
{
document.getElementById("project_LegalPoint").val
= 9;
 }

else
if
(mySelection ==
’2′)
{document.getElementById("project_LegalPoint").val
= 18;
}

else {document.getElementById("project_LegalPoint").val
= 27;
}

</script>

"project_LegalPoint"
is the ID of field "LegalPoint"

 

Hi,

Try updating val to value in js and expecting legalPoint is a hiddenfield.

document.getElementById("project_LegalPoint").value
= 9;

Check for errors in browser, if any.

Leave a Reply