简体   繁体   中英

can we pass entire model to javascript asp.net mvc

I have a problem that on javascript call for the form submit, the model gets updated from controller,but it is not updating in the view. I am thinking to update the model to new model values in javascript. so that the view shows the latest model values can that be done?

thanks, michael

Your question is extremely unclear and you provided no source code which makes things even more unclear. From the various comments you may have posted I assume that you are trying to update some model value inside the POST action without removing it from the model state and when the same view is rendered again the old values are displayed.

So I suppose you have a view model that looks something close to this:

public class MyViewModel
{
    public HttpPostedFileBase File { get; set; }
    public string SomeValue { get; set; }
}

and a controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            SomeValue = "initial value"
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        // Notice how the SomeValue property is removed from the
        // model state because we are updating its value and so that
        // html helpers don't use the old value
        ModelState.Remove("SomeValue");
        model.SomeValue = "some new value";
        return View(model);
    }
}

and a view:

<% using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
    <div>
        <%= Html.LabelFor(x => x.SomeValue) %>
        <%= Html.EditorFor(x => x.SomeValue) %>
    </div>
    <div>
        <label for="file">Attachment</label>
        <input type="file" name="file" />
    </div>
    <input type="submit" value="OK" />
<% } %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM