0

I just learned MVC3 from an example but I haven't found what I'm looking for. Something like in ASP.NET WebForms:

public void Something()
{
    string a = TextBoxA.Text;
    string b = TextBoxB.Text;
    TextBoxC.Text = a + b;
}

How to do that in MVC ? I tried to create an ActionResult but I don't want to redirect to another View.

3
  • you can use json/ajax call to the action present in the controller, make that controller return a value and then use that value on the view, so you see no post back is done by doing this way... I had implemented this, you can have a look at yassershaikh.com/… Commented Jun 11, 2012 at 7:58
  • Or even better, you could accomplish all this using only JavaScript (jQuery) on client side. Also take a look at Knockout.js. It's fairly simple to use, especially given your example. Commented Jun 11, 2012 at 10:06
  • Thank you very much. It will help me a lot. Commented Jun 11, 2012 at 17:21

1 Answer 1

2

You might want to do this @ client side i.e. using jQuery etc. In MVC, it has to be managed at controller side updating required properties of viewModel. That viewmodel intern is binded with controls on actual view

//Model
public class AddViewModel
{
    public int One { get; set; }
    public int Two { get; set; }
    public int Result { get; set; }
}
//Controller
public ActionResult Index()
{
    AddViewModel obj = new AddViewModel();
    obj.One = 1;
    obj.Two = 2;
    obj.Result = obj.One + obj.Two;
    return View(obj);
}
//View
@model MvcApplication3.Models.AddViewModel
@Html.EditorFor(model => model.One)
@Html.EditorFor(model => model.Two)
@Html.EditorFor(model => model.Result)
<input type="submit" value="Save" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.